The main topic: give some words, and some sentences, when and only if the sentence can be divided into a substring can be translated by the dictionary, it means that the substring can be translated. The longest prefix length that can be translated.
Idea: Use the trie tree to brush the array, the longest can be brushed is the longest place to translate the string.
PS: On the Bzoj trie incredibly faster than AC automata, my slag code to brush to the first article ...
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std struct trie{Trie *son[27]; BOOL End; Trie () {memset (son,null,sizeof (son)); end = false; }}*root = new Trie (); int Words,cnt;char s[1 << 20|100],temp[20];bool f[1 << 20|100]; inline void Insert (char *s) {Trie *now = root; while (*s! = ')} {if (Now->son[*s-' a '] = = NULL) now->son[*s-' a '] = new Trie (); now = now->son[*s-' a ']; ++s; } Now->end = true;} inline void Ask (char *s,int i) {Trie *now = root; int t = 0; while (*s! = ')} {if (Now->son[*s-' a '] = = NULL) return; now = now->son[*s-' a ']; ++s; ++t; if (now->end) F[i + t] = true; } if (now->end) F[i + t] = true;} inline int work () {memset (f,false,sizeof (f)); F[0] = true; int re = 0,length = strlen (s + 1); for (int i = 0; I <= leNgth; ++i) {if (!f[i]) continue; re = i; Ask (s + i + 1,i); } return re;} int main () {cin >> words >> CNT; for (int i = 1; I <= words; ++i) {scanf ("%s", temp); Insert (temp); } for (int i = 1; I <= cnt; ++i) {scanf ("%s", S + 1); printf ("%d\n", Work ()); } return 0;}
Bzoj 1212 Hnoi 2004 L Language Trie tree