USACO 2.3.1 Longest Prefix 解题报告
2011-07-16 21:33
453 查看
今天完全不行啊,USACO只做了一题,今天也只刷了两题,USACO还卡题了
Longest Prefix
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
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.
Longest Prefix
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; }
相关文章推荐
- USACO Section2.3 Longest Prefix 解题报告 【icedream61】
- USACO :Longest Prefix解题报告
- USACO 2.3.1 Longest Prefix --- Trie
- USACO2.3.1 The Longest Prefix (prefix)
- [LeetCode] Longest Common Prefix 解题报告
- 【LeetCode】Longest Common Prefix 解题报告
- Longest Common Prefix解题报告
- LeetCode解题报告--Longest Common Prefix
- USACO 2.3.1The Longest Prefix
- USACO 2.3.1 Longest Prefix --- Trie
- Longest Common Prefix ——解题报告
- USACO 2.3.1 Longest Prefix
- USACO 2.3.1 The Longest Prefix
- Longest Prefix_usaco 2.3.1_dp
- USACO2.3.1 The Longest Prefix 题解
- Leetcode Longest Common Prefix 解题报告
- USACO 2.3.1 Longest Prefix 最长前缀
- [LeetCode] Longest Common Prefix 解题报告
- USACO 2.3.1 Longest Prefix dp
- [LeetCode] Longest Common Prefix 解题报告