Hdu 6096 --- String (AC automatic mechanism), hdu6096 --- string

Source: Internet
Author: User

Hdu 6096 --- String (AC automatic mechanism), hdu6096 --- string

Question Link

 

Problem DescriptionBob has a dictionary with N words in it.
Now there is a list of words in which the middle part of the word has continuous letters disappeared. The middle part does not include the first and last character.
We only know the prefix and suffix of each word, and the number of characters missing is uncertain, it cocould be 0. But the prefix and suffix of each word can not overlap.
For each word in the list, Bob wants to determine which word is in the dictionary by prefix and suffix.
There are probably used answers. You just have to figure out how many words may be the answer.

 

InputThe first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two integer N and Q, The number of words in the dictionary, and the number of words in the list.
Next N line, each line has a string Wi, represents the ith word in the dictionary (0 <| Wi | ≤100000)
Next Q line, each line has two string Pi, Si, represents the prefix and suffix of the ith word in the list (0 <| Pi |, | Si | ≤ 100000,0 <| Pi | + | Si | ≤ 100000)
All of the above characters are lowercase letters.
The dictionary does not contain the same words.

Limits
T ≤ 5
0 <N, Q ≤ 100000
Σ Si + Pi ≤ 500000
Σ Wi ≤ 500000
 

 

OutputFor each test case, output Q lines, an integer per line, represents the answer to each word in the list.

 

Sample Input14 4 abacdeacdefacdefa acd efac ace f

 

Sample Output2110 question: there are n strings consisting of lower-case letters. Now I have q queries, each time I give a prefix and suffix, how many of the n strings meet the given prefix and suffix? Cannot the prefix and suffix overlap in this string? Train of Thought: For n strings s [I], change to s [I] = s [I] + "_" + s [I], for the prefix pre and suffix suf entered in each query, modify it to str = suf + "_" + pre, and construct the tire tree for the string consisting of the prefix and suffix of the q query, then, use the AC automatic machine to match and run it. But there will be redundancy, for example, aaa, prefix aa suffix aa, then it will also match, so you have to judge the length when comparing. The Code is as follows:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <string> # include <queue> # include <vector> using namespace std; const int N = 1e5 + 5; string s [N]; struct Node {Node * son [30]; Node * fail; int flag; int len ;} tr [6 * N]; Node * root; queue <Node *> Q; int ans [N]; vector <int> v [N]; int tot; void init () {tot = 0; memset (ans, 0, sizeof (ans); root = & tr [0]; while (! Q. empty () Q. pop (); for (int I = 0; I <N; I ++) v [I]. clear (); for (int I = 0; I <6 * N; I ++) {tr [I]. flag = 0; tr [I]. fail = NULL; for (int j = 0; j <30; j ++) tr [I]. son [j] = NULL ;}} void build (string s, int id) {Node * now = root; for (int I = 0; I <s. length (); I ++) {int x = s [I]-'_'; if (! Now-> son [x]) now-> son [x] = & tr [++ tot]; now = now-> son [x];} if (now-> flag) {v [now-> flag]. push_back (id); return;} now-> flag = id; now-> len = s. length ();} void setFail () {Q. push (root); while (! Q. empty () {Node * now = Q. front (); Q. pop (); for (int I = 0; I <30; I ++) {if (now-> son [I]) {Node * p = now-> fail; while (p &&(! (P-> son [I]) p = p-> fail; now-> son [I]-> fail = (p )? P-> son [I]: root; Q. push (now-> son [I]);} else now-> son [I] = (now! = Root )? Now-> fail-> son [I]: root ;}} void query (string s) {Node * now = root; int len = s. length (); for (int I = 0; I <len; I ++) {int x = s [I]-'_'; now = now-> son [x]; Node * p = now; while (p! = Root) {if (p-> flag & p-> len <= len/2 + 1) ans [p-> flag] ++; p = p-> fail ;}}int main () {int T; cin >>t; while (T --) {init (); int n, q; scanf ("% d", & n, & q); for (int I = 1; I <= n; I ++) {cin> s [I]; s [I] = s [I] + "_" + s [I];} for (int I = 1; I <= q; I ++) {string s1, s2; cin> s1> s2; string ss = s2 + "_" + s1; build (ss, i);} setFail (); for (int I = 1; I <= n; I ++) {query (s [I]);} for (int I = 1; I <= q; I ++) // process the same prefix suffix; {for (int j = 0; j <v [I]. size (); j ++) ans [v [I] [j] = ans [I];} for (int I = 1; I <= q; I ++) printf ("% d \ n", ans [I]);} 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.