NYOJ 1085 words (AC automatic machine template), nyoj1085
Several words time limit: 1000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
In order to successfully pass the CET-4 and CET-6, everyone now gets up early every morning to read English. LYH thought that he could pass level 6 in the June test, but did not expect that he did not pass the test after the score was obtained. So he had to spend more time learning English. To pass level 6, the most basic requirement is the vocabulary. To better remember some unfamiliar words, LYH sometimes finds some English articles to read. This morning, LYH found another article. Before reading, he suddenly came up with an idea: Which words appear the most frequently in the article?
-
Input
-
Enter an integer T in the first line, indicating that there are T groups of test data (1 ≤ T ≤ 200 ).
For each group of test data, enter an integer n (1 ≤ n ≤ 150) in the first line to indicate the number of words to be queried by LYH (some words may appear repeatedly ).
In the next n rows, enter a word in each line, and the length cannot exceed 100.
The last line contains an English article (string) consisting of lowercase letters. The length cannot exceed 10 ^ 6.
-
Output
-
For each group of data, the first line outputs an integer, indicating the number of times a word appears.
Then, in the input order, each row outputs a word with the most occurrences. Output all repeated words.
-
Sample Input
-
23goodooonegoodafternooneveryone1towelcometotopcoder
-
Sample output
-
2ooone2to
Analysis: This is an AC automatic machine template question. Note that a word may appear multiple times in a query. Here, we need to process it.
# Include <cstring> # include <cstdio> # include <algorithm> # include <map> # include <string> # include <queue> using namespace std; # define SIGMA_SIZE 26 // text string character content # define MAXNODE 20000 // number of nodes # define TEXT_SIZE 1000005 // text string length # define P_SIZE 100 // mode string length # define P_NUM 200 // Number of mode strings map <string, int> mp; struct AhoCorasickAutomata {int cnt [P_NUM]; int sz; int ch [MAXNODE] [SIGMA_SIZE]; int f [MAXNODE]; int val [MAXNODE]; Int last [MAXNODE]; void Init () {sz = 1; memset (ch [0], 0, sizeof (ch [0]); memset (cnt, 0, sizeof (cnt); mp. clear () ;}int idx (char c) {return c-'A';} void Insert (char * s, int v) {int u = 0, n = strlen (s); for (int I = 0; I <n; I ++) {int c = idx (s [I]); if (! Ch [u] [c]) {memset (ch [sz], 0, sizeof (ch [sz]); val [sz] = 0; ch [u] [c] = sz ++;} u = ch [u] [c];} val [u] = v; mp [string (s)] = v ;}void print (int j) {if (j) {cnt [val [j] ++; print (last [j]) ;}} void Find (char * T) {int n = strlen (T); int j = 0; for (int I = 0; I <n; I ++) {int c = idx (T [I]); while (j &&! Ch [j] [c]) j = f [j]; j = ch [j] [c]; if (val [j]) print (j ); else if (last [j]) print (last [j]) ;}} void Get_Fail () {queue <int> q; f [0] = 0; for (int c = 0; c <SIGMA_SIZE; c ++) {int u = ch [0] [c]; if (u) {f [u] = 0; q. push (u); last [u] = 0 ;}} while (! Q. empty () {int r = q. front (); q. pop (); for (int c = 0; c <SIGMA_SIZE; c ++) {int u = ch [r] [c]; if (! U) continue; q. push (u); int v = f [r]; while (v &&! Ch [v] [c]) v = f [v]; f [u] = ch [v] [c]; last [u] = val [f [u]? F [u]: last [f [u] ;}}}; char text [TEXT_SIZE]; char P [P_NUM] [P_SIZE]; AhoCorasickAutomata ac; int n, t; int main () {scanf ("% d", & T); int cas = 0; while (T --) {scanf ("% d", & n ); ac. init (); for (int I = 1; I <= n; I ++) {scanf ("% s", P [I]); ac. insert (P [I], I);} ac. get_Fail (); scanf ("% s", text); ac. find (text); int Max_cnt =-1; for (int I = 1; I <= n; I ++) if (ac. cnt [I]> Max_cnt) Max_cnt = ac. cnt [I]; printf ("% d \ n", Max_cnt); for (int I = 1; I <= n; I ++) if (ac. cnt [mp [string (P [I])] = Max_cnt) printf ("% s \ n", P [I]);} return 0 ;}
Ac pascal Template
POJ1204 (AC automatic machine template)
Give A string whose N president is M, give you some strings to be matched, start from any string can have eight directions, up to A, clockwise is A--H, ask how the coordinates of the matched string in the N * M string are.
Code:
Const maxnodes = 500000;
Var fx: array [1 .. 8] of char = ('E', 'F', 'G', 'h', 'A', 'B', 'C', 'D ');
T: array [0 .. maxnodes, 'A'... 'Z'] of longint;
F, q, w: array [0 .. maxnodes] of longint;
E: array [0 .. 1001, 0 .. 1001] of char;
S: array [0 .. 1001] of char;
Colu, line: array [0 .. 1] of longint;
Done: array [0 .. 1001] of boolean;
Ans: array [0 .. 1001] of record x, y, f: longint; end;
N, m, num, u, I, j, k, l, r, root, size, x, y, p, li, co, tmp, len: longint;
C: char;
Function nx (var x, y: longint; f: longint): char;
Begin
Case f
1: dec (x );
2: begin dec (x); inc (y); end;
3: inc (y );
4: begin inc (x); inc (y); end;
5: inc (x );
6: begin inc (x); dec (y); end;
7: dec (y );
8: begin dec (x); dec (y); end;
End;
If (x <1) or (x> n) or (y <1) or (y> m) then exit ('! ');
Exit (e [x, y]);
End;
Begin
Readln (n, m, num );
Line [0]: = 1;
Line [1]: = n;
Colu [0]: = 1;
Colu [1]: = m;
For I: = 1 to n do
Begin
For j: = 1 to m do read (e [I, j]);
Readln;
End;
Root: = 0;
Size: = 0;
For I: = 1 to num do
Begin
Len: = 0;
While not eoln do
Begin
Inc (len);... the remaining full text>
How to Implement the AC automatic mechanism in ACM
I learned about DFA, but it also hurts.
Set status transfer no problem
It is hard to understand the inverse Recursive Method of DFA.
No better way.
My method is to simulate the process of changing the state transfer.
But it will be troublesome.
In fact, some problems can be hard to understand at a time. For example, when we only access 2-SAT, and when we are using network streams, we may continue to learn more slowly.