The structure of some biological objects is represented by the sequence of their constituents, where each part is denoted by an uppercase letter. Biologists are interested in decomposing a long sequence into shorter sequences 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
题意:给一个集合和一串字母,问这串字母中由集合中子集构成的最大前缀和。
一步步的往后走就可以了。
代码:
/*
ID:iam666
PROG:prefix
LANG:C++
*/
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int tmp;
char a[210][20];
string str;
int main (void)
{
freopen("prefix.in","r",stdin);
freopen("prefix.out","w",stdout);
//printf("helaaaalo\n");
int cnt=0,Max=0;
while(cin>>a[cnt]&&a[cnt++][0]!='.');
str="";
string s;
while(cin>>s)
str+=s;
for(int i=0;i<str.length();i++)
{
for(int j=0;j<cnt;j++)
{
if(i+strlen(a[j])<=str.length());
{
bool jud=true;
for(int k=0;k<strlen(a[j]);k++)
{
if(str[i+k]!=a[j][k])
{
jud=false;
break;
}
}
if(jud)
{
if(Max<i+strlen(a[j]))
Max=i+strlen(a[j]);
}
}
}
if(i+1>Max)
break;
}
printf("%d\n",Max);
return 0;
}