USACO 2.3.1 Longest Prefix 解题报告
今天完全不行啊,USACO只做了一题,今天也只刷了两题,USACO还卡题了
IOI'96
The structure of some biological objects is represented by the sequence of their constituents denoted by uppercase letters. 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 exceed 76 letters in length. The "newlines" 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
题目大意:就是给出一个字符串,求最大前缀的长度
可以用DP+字典树求解
/* ID:shiryuw1 PROG:prefix LANG:C++ */ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int MAX=5020; char str[200005]={0}; struct progtrie{ int a[26]; bool hash; }trie[MAX]; int tree=1; int maxpre=-1; int vis[200005]={0}; bool isin(char *ch) { int i; int k=0; for(i=0;ch[i]!=0;i++) { k=trie[k].a[ch[i]-'A']; if(k==0) { return false; } } if(k==0||trie[k].hash==false) return false; return true; } int main() { freopen("prefix.in","r",stdin); freopen("prefix.out","w",stdout); int i,ii; for(i=0;i<MAX;i++) { for(ii=0;ii<26;ii++) { trie[i].a[ii]=0; } trie[i].hash=false; } while(1) { char ch[20]; cin>>ch; if(ch[0]=='.') break; int k=0; for(i=0;ch[i]!=0;i++) { if(trie[k].a[ch[i]-'A']==0) { trie[k].a[ch[i]-'A']=tree; tree++; } k=trie[k].a[ch[i]-'A']; } trie[k].hash=true; } getchar(); char ch[100]={0}; while(cin>>ch) { strcat(str,ch); } bool ans=true; int len=strlen(str); int maxlength=0; for(i=1;i<=len;i++) { int length=0; int j; for(j=1;j<=10;j++) { int x; if(i>=j) { if(vis[i-j]!=(i-j)) continue; ans=false; int y=0; for(x=i-j;x<i;x++) { ch[y]=str[x]; y++; } ch[y]=0; if(isin(ch)) { if(vis[i-j]+y>length) { length=vis[i-j]+y; } } } } vis[i]=length; if(length>maxlength) maxlength=length; if(ans) break; } cout<<maxlength<<endl; return 0; }
作者:ACShiryu
出处:http://www.cnblogs.com/ACShiryu/
若非注明,本博客文章均为原创,版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
该文章也同步发布在我的新浪微博中-ACShiryu's weibo,欢迎收听。