Description
In biology, some biological structures are represented by an uppercase letter sequence containing its elements. Scientists are interested in breaking down long sequences into short sequences (I .e. elements.
If an element in a set of P can form a sequence s through concatenation (elements can be reused, which is equivalent to the "+" Operator in Pascal), we think that the sequence s can be decomposed into elements in P. The element does not have to appear in all cases (for example, the BBC does not appear ). For example, the sequence ababacabaab can be divided into elements in the following set:
{A, AB, Ba, CA, BBC}
The first k characters of the sequence s are called the prefixes whose lengths are K. DesignProgramInput an element set and an uppercase letter sequence S, and set S' to the longest prefix of the sequence s so that it can be decomposed into elements in the given set p, evaluate the length of s k.
Format
Program name: Prefix
Input Format
The input data starts with a set of 1 .. 200 elements (Length: 1 .. 10) and is represented in consecutive strings separated by spaces. All letters are in uppercase, and there may be more than one row of data. The ending sign of the element set is a row that contains only one. The elements in the set are not repeated. Next is the uppercase letter sequence s, which is 1 .. 200,000 in length. It is represented by a string of one or more lines. Each line cannot exceed 76 characters. Line breaks are not part of series S.
Output Format
Only one row outputs an integer, indicating the maximum length of S's qualified prefix.
Sample input (File prefix. In)
A AB ba ca BBC. ababacabaabc
Sample output (File prefix. out)
11
Analysis: At first, I thought that trie + DFS + KMP is a good example. Unfortunately, it's just a pity that this topic is too inefficient and decisive. TLE;
After that, the idea of using DP is relatively simple. DP [I] indicates whether the current location is reachable,
State transition equation: DP [I + Len [J] = DP [I]; (0 <= j <m) J indicates the elements in the current dictionary
# Include <iostream> # include <algorithm> # include <string. h ># include <fstream> using namespace STD; struct DIC {char STR [11]; int Len;} d [210]; char s [200100]; bool DP [200200] = {0}; bool CMP (dic a, dic B) {return. len> B. len;} int main () {freopen ("prefix. in "," r ", stdin); freopen (" prefix. out "," W ", stdout); char STR [11], str2 [80]; int m = 0; while (scanf (" % s ", STR) = 1) {If (strcmp (STR ,". ") = 0) break; strcpy (d [M]. STR, STR); D [M ++]. len = s Trlen (STR);} // sort (D, D + M); s [0] = '\ 0'; while (CIN> str2) strcat (S, str2); int len1 = strlen (s); DP [0] = 1; int ans = 0; For (INT I = 0; I <len1; I ++) {If (! DP [I]) continue; For (Int J = 0; j <m; j ++) {int flag = 0; For (int K = 0; k <D [J]. len; k ++) {If (d [J]. STR [k]! = S [I + k]) {flag = 1; break ;}} if (! Flag) {DP [I + d [J]. len] = 1; if (I + d [J]. len> ans) ans = I + d [J]. len ;}}} cout <ans <Endl ;}
Below is the first timeoutCode
# Include <iostream> # include <algorithm> # include <fstream> # include <string. h> using namespace STD; typedef struct node {node * Next [26]; int V;} * tree, T; tree root; int ans; char STR [200010]; void insert (char * s) {tree P = root, newnode; For (; * s! = '\ 0'; s ++) {int d = * s-'A'; If (p-> next [d]! = NULL) P = p-> next [d]; else {newnode = (tree) malloc (sizeof (t); For (INT I = 0; I <26; I ++) newnode-> next [I] = NULL; newnode-> V =-1; p-> next [d] = newnode; P = newnode ;}} p-> V = 1;} void DFS (char * s, int CNT) {tree P = root; char * S1 = s; If (CNT> ans) ans = CNT; // cout <S <Endl; // cout <CNT <Endl; For (INT I = 1; * S1! = '\ 0'; S1 ++, I ++) {int d = * S1-'A'; P = p-> next [d]; if (P = NULL) return; // cout <D <''<p-> v <Endl; If (p-> V = 1) {// cout <"long" <I <Endl; DFS (S + I, CNT + I) ;}} int main () {char str1 [20], str2 [76]; root = (tree) malloc (sizeof (t); For (INT I = 0; I <26; I ++) root-> next [I] = NULL; root-> V =-1; freopen ("prefix. in "," r ", stdin); freopen (" prefix. out "," W ", stdout); While (scanf (" % s ", str1) = 1) {If (strcmp (str1 ,". ") = 0) break; insert (str1);} ans = 0; STR [0] = '\ 0'; while (CIN> str2) strcat (STR, str2); // CIN> STR; DFS (STR, 0); cout <ans <Endl; return 0 ;}