Toot Toot
Face: Encrypt one sentence: 1. All letters become lowercase. 2. Flip all the words. 3. Remove the space. And then give you an encrypted string and some appear in the original sentence and does not appear in the original sentence of the word, let you restore the original sentence. Note that each word can be used more than once, and if there are multiple answers, output any one of them.
Trie tree Good question ...
The first thing you can think of is to put all the words into a trie tree, and then I think, for the encrypted string, every word every character as the end of the word, and then run backwards trie the tree, know that encountered a word, record is the first few, the last output.
But this is wrong, because there is no guarantee that after encountering a word, this word has been matched, for example: High, after the encryption is Hgih, and then the given word has hi and high, when the last character ' H ' is swept, the first word in the trie tree is Hi, But before Hi Gh can't make a word, so GG.
Therefore, we also have a pre array, which records the last occurrence of each match, which is transferred from the previous match point, when running on the trie tree, only the current point constitutes a word, and the first character of the word is a matching point before return.
Finally, follow the pre array to find the head, flashback output.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cmath>5#include <cstring>6#include <cstdlib>7#include <cctype>8#include <stack>9#include <queue>Ten#include <vector> One using namespacestd; A #defineEnter puts ("") - #defineSpace Putchar (") - #defineMem (A, X) memset (A, X, sizeof (a)) the #defineRG Register -typedefLong Longll; -typedefDoubledb; - Const intINF =0x3f3f3f3f; + ConstDB EPS = 1e-8; - Const intMAXN = 1e4 +5; + Const intMAXM = 1e5 +5; A inline ll read () at { -ll ans =0; - Charch = getchar (), Las =' '; - while(!isdigit (ch)) las = ch, ch =GetChar (); - while(IsDigit (ch)) ans = ans *Ten+ CH-'0', ch =GetChar (); - if(Las = ='-') ans =-ans; in returnans; - } toInlinevoidWrite (ll x) + { - if(X <0) Putchar ('-'), x =-x; the if(x >=Ten) Write (X/Ten); *Putchar (x%Ten+'0'); $ }Panax Notoginseng - intN, M; the CharS[MAXN], ss[maxm][1005]; + A intCNT =0; the structTrie + { - intch[ -], Val; $ Trie () $ { -Mem (CH,0); val =0; - } the}T[MAXM *Ten]; - intGetnum (Charc)Wuyi { the returnC >='a'? C'a'C'A'; - } Wu voidInsertintIdChar*s) - { About intLen =strlen (s); $ for(inti =0, now =0; i < Len; ++i) - { - intc =Getnum (S[i]); - if(!t[now].ch[c]) t[now].ch[c] = + +CNT; Anow =T[now].ch[c]; + if(i = = Len-1) T[now].val =ID; the } - } $ the intANS[MAXN], PRE[MAXN]; the voidQueryintx) the { the for(inti = x, now =0; I >=0; --i) - { in intc =Getnum (S[i]); the if(!t[now].ch[c])return; thenow =T[now].ch[c]; About if(T[now].val && (Ans[i-1] || i = =0)) {Pre[x] = i-1, ans[x] = T[now].val;return;} the } the return; the } + - the voidPrintintx)Bayi { the if(pre[x]! =-1) print (pre[x]); theprintf"%s", Ss[ans[x]]); - } - the intMain () the { then = read (); scanf"%s", s); them =read (); - for(inti =1; I <= m; ++i) the { thescanf"%s", Ss[i]); the Insert (i, ss[i]);94 } the for(inti =0; I < n; ++i) query (i); thePrint (N-1); Enter; the return 0;98}
View Code
cf633c Spy syndrome 2