NYOJ 1085 數單詞 (AC自動機模板題),nyoj1085

來源:互聯網
上載者:User

NYOJ 1085 數單詞 (AC自動機模板題),nyoj1085
數單詞時間限制:1000 ms  |  記憶體限制:65535 KB難度:4

描述
為了能夠順利通過英語四六級考試,現在大家每天早上都會早起讀英語。LYH本來以為自己在6月份的考試中可以通過六級,可是沒想到,成績出來以後,居然沒有通過。所以他不得不付出更多的時間來學習英語。要想通過六級,最基本的要求就是詞彙量。為了能夠更快的記住一些陌生單詞,LYH有時會找一些英語文章來讀。今天早上,LYH又找了一篇文章。讀之前,他突然萌生出一個想法:文章中哪些單詞出現的次數最多呢?
輸入
第一行輸入一個整數T,表示有T組測試資料(1≤T≤200)。
對於每組測試資料,第一行輸入一個整數n(1≤n≤150),表示LYH要查詢的單詞數量(有些單詞可能會重複出現)。
接下來n行,每行輸入一個單詞,長度不大於100。
最後一行包含一個由小寫字母組成的英語文章(字串),長度不大於10^6。
輸出
對於每組資料,第一行輸出一個整數,表示單詞出現的次數。
然後按照輸入順序,每行輸出一個出現次數最多的單詞。如果有重複出現的單詞,把它們全部輸出。
範例輸入
23goodooonegoodafternooneveryone1towelcometotopcoder
範例輸出
2ooone2to

分析:這就是一個AC自動機模板題,要注意的是查詢的單詞中,一個單詞可能會出現多次,這裡要處理一下。

#include <cstring>#include <cstdio>#include <algorithm>#include <map>#include <string>#include <queue>using namespace std;#define SIGMA_SIZE 26 //文本串字元內容#define MAXNODE 20000 //節點數量#define TEXT_SIZE 1000005 //文本串長度#define P_SIZE 100 //模式串長度#define P_NUM 200 //模式串數量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模板

POJ1204(AC自動機模板題)
題意:給一個N行長為M的字串,給你一些需要去匹配的字串,從任意一個字串開始可以有八個方向,向上為A,順時針依次是A——H,問你去匹配的字串在給你的N*M字串中的座標是怎麼樣的。
代碼:
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 of
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);......餘下全文>>
 
ACM裡面的AC自動機怎搞

擦,關於DFA我學的時候也很蛋疼
設定狀態轉移沒有問題
很不能理解DFA的反著遞推的方式
也沒的更好的方法
對狀態轉移的那部分式子的理解,我的方法就是進行類比,看下變化的過程
不過會比較麻煩
其實有些問題一時間不能理解也很正常,比如才接觸2-SAT,網路流的時候那樣,可能持續的學習就會慢慢對的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.