1 条题解

  • 0
    @ 2021-6-14 23:29:21

    C++ :

    #include<cstdio>
    #include<cstring>
    int word[201][201],dp[201][201][41];
    char c[21],w[6][10],c0[201],c1[201];
    int d,p,k,s,Max,le[6],len;
    int main(){
    	int i,j,l,m,yes,x,st;
    		scanf("%d%d",&p,&k);
      		for(j=0;j<p;j++){
       			scanf("%s",c);
       			if(!j)strcpy(c0, c);
       			else strcat(c0, c);
      		}
      		len=strlen(c0);
      		scanf("%d",&s);
      		for(j=0;j<s;j++){
       			scanf("%s",w[j]);
       			le[j]=strlen(w[j]);
      		}
      		for(i=0;i<len;i++)for(j=0;j<len;j++)word[i][j]=0; 
      		for(i=len-1;i>=0;i--)for(j=len-1;j>=0;j--){
        			for(l=0;l<s;l++){
         				yes=0;
         				if(c0[j]==w[l][0]&&le[l]<=i-j+1){
          					yes=1; 
          					for(m=0;m<le[l];m++)
           						if(c0[j+m]!= w[l][m]){yes=0;break;}
         				}
         				if(yes==1)break;
        			}
        			if(yes==1)word[j][i]=word[j+1][i]+1; 
        			else word[j][i]=word[j+1][i];
       			}
      		for(st=1;st<=k;st++)for(i=0;i<len-st+1;i++)for(j = i+st-1; j < len; j++){
         				if(st==1){
    						dp[i][j][st]=word[i][j]; 
    						continue; 
    					}
         			for(Max=0,l=i+st-2;l<j;l++){
          				x=dp[i][l][st-1]+word[l+1][j];
          				if(x>Max)Max=x;
         			}
         			dp[i][j][st]=Max;
        			}
      		printf("%d\n",dp[0][len-1][k]);
     	return 0;
    }
    

    Pascal :

    {$r-,q-,s-,n+,g+}
    const
    	maxn = 205;
    	maxk = 41;
    var
    	p,n,k,caseno:integer;
    	s,tmps:string[maxn];
    	mlen:array[1..maxn] of integer;
    	word:array[1..6] of string;
    	g:array[0..maxn,0..maxn] of integer;
    	h:array[0..maxn,0..maxk] of integer;
    	i,j,u,v:Integer;
    
    	begin
    		begin
    			dec(caseno);
    			
    			{ init; }
    			readln(p,k);
    			n:=p*20;
    			s:='';
    			for i:=1 to p do
    			begin
    				readln(tmps);
    				s:=s+tmps;
    			end;
    			readln(p);
    			for i:=1 to p do
    				readln(word[i]);
    			
    			{ precalc mlen }
    			for i:=1 to n do
    			begin
    				mlen[i]:=maxn;
    				for j:=1 to p do
    					if( (pos(word[j],copy(s,i,length(s)-i+1))=1) and (length(word[j])<mlen[i]) ) then
    						mlen[i]:=length(word[j]);
    			end;
    				
    			{ precalc g }
    			for i:=1 to n do
    				for j:=1 to n do
    				begin
    					g[i][j]:=0;
    					for u:=i to j do
    						if(u+mlen[u]-1<=j) then
    							inc(g[i][j]);
    				end;
    			
    			{ Dynamic Programming }
    			fillchar(h,sizeof(h),0);
    			for j:=1 to k do
    				for u:=j to n do
    					for v:=u to n do
    						if(h[u-1][j-1]+g[u][v]>h[v][j])then
    							h[v][j]:=h[u-1][j-1]+g[u][v];
    			writeln(h[n][k]);
    		end;
    		
    		close(input);
    	
    	end.
    
    • 1

    信息

    ID
    213
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者