Prefix Problem Solving report
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
Give you many substrings p and a long string s, what is the longest length of the prefix in s that can be combined with these substrings (which can be reused)?
"Data Range"
The length of p is within 1~10 and the number of substrings within 1~200
The length of S is within 1~200,000
"Input Format"
First, we give the individual substring p, separated by blanks, with a single line of '. ' Character as Terminator
Then give S, divided by multiple lines, with no more than 76 characters per line. Note that the line break does not belong to S.
"Input Sample"
A AB BA CA BBC
.
Ababacabaabc
"Output Example"
11
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
A simple DP.
Input and output not much to say, the bool type array d, defined as follows: D[i]==ture indicates that the s-length prefix for I can be represented by P.
As a result, the d[i+p[j].size () can be updated from d[i] until the end of the update is pushed down.
Assuming there are k substrings, s string length is n, then the total time complexity is O (NK).
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
There is a very serious problem with my code last point when the run time is 0.99s~1.04s between fluctuations, so may time out, possibly AC ... This means that my algorithm should be improved, or constant optimization, especially to remind the reader!
In addition, during the submission process, there are several details that lead to the absence of AC at once. This is not mentioned here, because these errors are not useful for reference, just because I wrote the question is half, put a week, and then continue to finish ... So there are so few mistakes.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:prefix4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One intK; A stringp[201],s; - - BOOLd[200001]; the - intMain () - { -Ifstreaminch("prefix.in"); +Ofstream out("Prefix.out"); - + stringstr; A for(inch>>str;str!=".";inch>>STR) p[++k]=str; at //cout<< "k=" <<K<<endl; - //for (int i=1;i<=k;++i) cout<< "p[" <<i<< "]=" <<P[i]<<endl; - while(inch>>STR) s+=str; -s="."+S; - //cout<< "12345678901234567890123456789012345678901234567890123456789012345678901234567890"; - //cout<< "s.size=" <<s.size () <<endl; in //cout<<s<<endl; - tod[0]=true; + for(intI=0; I!=s.size (); + +i) - if(D[i]) the { * //cout<< "d[" <<i<< "]=true" <<endl; $ for(intj=1; j<=k;++j)Panax Notoginseng if(S.find (p[j],i+1) ==i+1) d[i+p[j].size ()]=true; - } the for(intI=s.size (); i>=0;--i) + if(D[i]) { out<<i<<endl; Break; } A the inch. Close (); + out. Close (); - return 0; $}
Usaco Section2.3 Longest Prefix Problem solving report "Icedream61"