【HDU】病毒侵襲(AC自動機模板題),hdu自動機

來源:互聯網
上載者:User

【HDU】病毒侵襲(AC自動機模板題),hdu自動機

AC自動機的模板題,由於輸入的字串中的字元不保證全為小寫字母,所以範圍應該在130之前,而前31位字元是不可能出現在字串的(不懂得查下ACSII表就行了),所以只需要開的結點數組大小為130足夠了,如果開256就會記憶體超限。

11908775 2014-10-19 10:45:38 Accepted 2896 250MS 29596K 2760 B G++ KinderRiven

輸入只有一組,所以不用擔心逾時的問題。

#include<queue>#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int maxn = 111111;const int max_size = 130;const int maxd = 222;vector<int>G[1111];struct Trie{    int next[maxn][max_size];    int fail[maxn];    int  val[maxn];    int sz;    int root;    int index(char e){        return e - 31;    }    void init(){        sz = 0;        root = newnode();    }    int newnode(){        val[sz] = 0;        memset(next[sz],-1,sizeof(next[sz]));        sz ++;        return sz - 1;    }    void insert(char *str,int pos){        int L = strlen(str);        int u = root;        for(int i = 0; i < L; i++){            int e = index(str[i]);            if(next[u][e] == -1)               next[u][e] = newnode();           u = next[u][e];        }        val[u] = pos;        //printf("%d %d\n",u,pos);    }    void build(){ //建立失配邊        fail[root] = root;        queue<int>q;        for(int i = 0; i < max_size; i++){            if(next[root][i] == -1)               next[root][i] = root;            else{                fail[next[root][i]] = root;                q.push(next[root][i]);            }        }        while(!q.empty()){            int now = q.front(); q.pop();            for(int i = 0; i < max_size; i++){                if(next[now][i] == -1)                   next[now][i] = next[fail[now]][i];                else{                   fail[next[now][i]] = next[fail[now]][i];                   q.push(next[now][i]);                }            }        }    }    void count(char *str,int pos){        int L = strlen(str);        int u = root;        for(int i = 0; i < L; i++){            int e = index(str[i]);            u = next[u][e];            int temp = u;            while(temp != root){                if(val[temp] != 0)                    G[pos].push_back(val[temp]);                temp = fail[temp];            }        }        return;    }};Trie ac;int main(){    int n,m;    ac.init();    scanf("%d",&n);    for(int i = 1; i <= n; i++){        char str[maxd];        scanf("%s",str);        ac.insert(str,i);    }    //printf("%d\n",ac.sz);    ac.build();    scanf("%d",&m);    for(int i  = 1; i <= m; i++){        char str[maxn];        scanf("%s",str);        ac.count(str,i);    }    int cnt = 0;    for(int i = 1; i <= m; i++)if(G[i].size()){        sort(G[i].begin(),G[i].end());        cnt ++;        printf("web %d:",i);        for(int j = 0; j < G[i].size(); j++)            printf(" %d",G[i][j]);        printf("\n");    }    printf("total: %d\n",cnt);}


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.