USACO2.3.1--Longest Prefix

Longest Prefix
IOI'96

The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in decomposing a long sequence into shorter ones called primitives.

We say that a sequence S can be composed from a given set of primitives P if there is a some sequence of (possibly repeated) primitives from the set whose concatenation equals S. Not necessarily all primitives need be present. For instance the sequence ABABACABAABcan be composed from the set of primitives

	   {A, AB, BA, CA, BBC}

The first K characters of S are the prefix of S with length K. Write a program which accepts as input a set of primitives and a sequence of constituents and then computes the length of the longest prefix that can be composed from primitives.

PROGRAM NAME: prefix

INPUT FORMAT

First, the input file contains the list (length 1..200) of primitives (length 1..10) expressed as a series of space-separated strings of upper-case characters on one or more lines. The list of primitives is terminated by a line that contains nothing more than a period (`.'). No primitive appears twice in the list. Then, the input file contains a sequence S (length 1..200,000) expressed as one or more lines, none of which exceeds 76 letters in length. The "newlines" (line terminators) are not part of the string S.

SAMPLE INPUT (file prefix.in)

A AB BA CA BBC

.

ABABACABAABC

OUTPUT FORMAT

A single line containing an integer that is the length of the longest prefix that can be composed from the set P.

SAMPLE OUTPUT (file prefix.out)

11
解题思路:看题目之后YY了一个DP方程,不过实现需要两个循环嵌套,所以对于极限数据要进行200000*200=40000000次,然后又因为此题是IOI的题目,所以觉得肯定不是那么简单,应该要进行优化。不过实在没想到怎么优化,所以写了个朴素的DP。居然AC了。。。然后看了下ANALYSIS,果断吐血。。。In the worst case, you have to check each prefix starting at each location. There are at most 200 prefixes of length 10 to check at each of 200,000 locations, for a total of 400,000,000 operations. This is a little higher than the standard number you would expect to be able to do in 5 seconds, but it is a very small operation (two array look-ups and a character comparison), and an over-estimate on how bad it can actually get.
题目的状态转移方程为:f[0]=1;

f[i]=f[j]+i-j(f[j]<>0)(实现的时候其实只要标记一下就行啦,感觉就是枚举)
View Code
 1 /*

 2 ID:spcjv51

 3 PROG:prefix

 4 LANG:C

 5 */

 6 #include<stdio.h>

 7 #include<string.h>

 8 #include<ctype.h>

 9 char pre[205][11],s[200005];

10 long f[200005];

11 int main(void)

12 {

13     freopen("prefix.in","r",stdin);

14     freopen("prefix.out","w",stdout);

15     long i,j,k,ans,len,maxlen,l;

16     char cc;

17     i=0;

18     while(scanf("%s",pre[i])!=EOF)

19     {

20         if(pre[i][0]=='.') break;

21         i++;

22     }

23     ans=i-1;

24     i=1;

25     maxlen=0;

26     while(scanf("%c",&cc)!=EOF)

27         if(isupper(cc))

28         {

29             s[i]=cc;

30             i++;

31         }

32 

33     len=i-1;

34     memset(f,0,sizeof(f));

35     f[0]=1;

36     for(i=1; i<=len; i++)

37     {

38         for(j=0; j<=ans; j++)

39         {

40             l=strlen(pre[j]);

41             if(i>=l)

42             {

43                 for(k=0; k<l; k++)

44                     if(s[i-l+1+k]!=pre[j][k]) break;

45                 if(k==l&&f[i-l])

46                 {

47 

48                     f[i]=1;

49                     if(i>maxlen)

50                         maxlen=i;

51                 }

52             }

53         }

54     }

55     printf("%ld\n",maxlen);

56     return 0;

57 }

 



你可能感兴趣的:(USACO)