1 solutions

  • 0
    @ 2024-12-11 0:49:30

    C :

    #include "stdio.h"
    #include "string.h"
    int m,n;
    char s[200000];
    char a[201][11];
    int d[201];
    int f[200001];
    void F(int x,int y)
    {
    	int i;
    	if(x+d[y]>m)
    		return;
    	for(i=0;i<d[y];i++)
    		if(s[x+i]!=a[y][i])
    			return;
    		f[x+d[y]]=1;
    }
    int main()
    {
    	int i,j;
    	char c;
    	scanf("%s",a[0]);
    	d[0]=strlen(a[0]);
    	n=0;
    	while(a[n][0]!='.')
    	{
    		n++;
    		scanf("%s",a[n]);
    		d[n]=strlen(a[n]);	
    	}
    	m=0;
    	while(~scanf("%c",&c))
    	{
    		if(c>='A'&&c<='Z')
    		{
    			s[m]=c;
    			m++;
    		}
    	}
    	s[m]=0;
    	f[0]=1;
    	for(i=0;i<m;i++)
    	{
    		if(f[i])
    			for(j=0;j<n;j++)
    				F(i,j);
    	}
    	for(i=m;i>=0;i--)
    	{	
    		if(f[i])
    		{
    			printf("%d\n",i);
    			break;
    		}
    	}
    	return 0;
    }
    

    C++ :

    /*
    
    PROB: prefix
    
    LANG: C++
    
    */
    
    #include <fstream>
    #include<iostream>
    using namespace std;
    int main()
    {
    
        //ifstream fin("prefix.in");
    
        //ofstream fout("prefix.out");
    
        string prim[201],str="",t;
    
        bool able[200001]={false};
    
        able[0]=true;
    
        int i,j,u,v,num=0;
    
        while(cin>>t&&t!=".") prim[num++]=t;
    
        while(cin>>t) str+=t;
    
        for(i=1;i<=str.size();i++)
    
        {
    
            for(j=0;j<num;j++)
    
            {
    
                if(prim[j].size()>i) continue;
    
                for(u=i-1,v=prim[j].size()-1;v>=0&&str[u]==prim[j][v];u--,v--);
    
                if(v<0&&able[i-prim[j].size()]) able[i]=true;
    
            }
    
        }
    
        for(i=str.size();!able[i];i--);
    
        cout<<i<<endl;
    
        return 0;
    
    }
    

    Pascal :

    program p22830;
    var a:array[0..203]of string;
        n,m,i,j,k,ans:longint;
        s:ansistring;
        st:string;
        f:array[0..200012]of boolean;
    function jq(l,r:longint):string;
    var i:longint;
    begin
     i:=l;
     jq:='';
     while i<=r do
      begin
       jq:=jq+s[i];
       inc(i);
      end;
    end;
    begin
     i:=1;
     j:=1;
     readln(s);
     while s[1]<>'.'do
      begin
       while j<=length(s) do
        begin
         if s[j]<>' ' then a[i]:=a[i]+s[j]
         else inc(i);
         inc(j);
        end;
       readln(s);
       j:=1;inc(i);
      end;
     if a[i]='' then n:=i-1
     else n:=i;
     s:='';
     while not eof do
      begin
       readln(st);
       s:=s+st;
      end;
     fillchar(f,sizeof(f),false);
     f[0]:=true;
     for i:=1 to length(s) do
      for j:=1 to n do
       if (f[i]) then continue
       else if (i-length(a[j])>=0)and(f[i-length(a[j])])
               and(a[j]=jq(i-length(a[j])+1,i))
            then f[i]:=true;
     ans:=0;
     for i:=0 to length(s) do
     if f[i] then ans:=i;
     writeln(ans);
    end.
    
    
    • 1

    Information

    ID
    752
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By