Usaco 2.3.1 longest prefix solution report

Source: Internet
Author: User
Document directory
  • Program name: prefix
  • Input Format
  • Sample input (File prefix. In)
  • Output Format
  • Sample output (File prefix. out)

It's totally unavailable today. usaco only has one question. Today, we only have two questions, and usaco is still stuck.

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 calledPrimitives.

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 sequenceAbabacabaabCan be composed from the set of primitives

   {A, AB, BA, CA, BBC}

The first k characters of s arePrefixS 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: prefixinput 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
It is to give a string and obtain the maximum prefix length.
It can be solved using the DP + dictionary tree
/*ID:shiryuw1PROG:prefixLANG: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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.