AmazingCounters.com

USACO Longest Prefix

题目大意:给出一个长字符串,问最长的前缀,使得这个前缀能用给出的一些元素组合而成

思路:暴力dp,dp[i]表示长度为i的前缀能否被表示

 

 1 /*{
 2 ID:a4298442
 3 PROB:prefix
 4 LANG:C++
 5 }
 6 */
 7 #include<iostream>
 8 #include<fstream>
 9 #include<cstring>
10 #include<algorithm>
11 #define maxn 109
12 using namespace std;
13 ifstream fin("prefix.in");
14 ofstream fout("prefix.out");
15 //#define fin cin
16 //#define fout cout
17 int dp[200009];
18 char premitive[maxn*3][maxn],ch[200009];
19 int check(int x,int y,int totle)
20 {
21     char temp[maxn],h=0;
22     for(int i=x;i<=y;i++)
23     {
24         temp[++h]=ch[i];
25     }
26     temp[h+1]='\0';
27     for(int i=1;i<=totle;i++)
28     {
29         if(strcmp(temp+1,premitive[i]+1)==0)return 1;
30     }
31     return 0;
32 }
33 int main()
34 {
35     int n,h=0;
36     while(1)
37     {
38         fin>>(premitive[++h]+1);
39         if(premitive[h][1]=='.'){h--;break;}
40     }
41     char c;
42     int len=0;
43     while(fin>>c)
44     {
45         if('A'<=c && c<='Z')ch[++len]=c;
46     }
47     ch[len+1]='\0';
48     dp[0]=1;
49     for(int i=1;i<=len;i++)
50     {
51         for(int j=max(i-10,1);j<=i;j++)
52         {
53             if(check(j,i,h)&& dp[j-1])
54             {
55                 dp[i]=1;break;
56             }
57         }
58     }
59     int ans=0;
60     for(int i=len;i>=1;i--)
61     {
62         if(dp[i]==1)
63         {
64             ans=i;
65             break;
66         }
67     }
68     fout<<ans<<endl;
69     return 0;
70 }

 

posted @ 2015-03-08 17:16  philippica  阅读(241)  评论(0编辑  收藏  举报