HDU 5384字典樹運(yùn)用
題面:
Danganronpa
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 512????Accepted Submission(s): 284
Problem Description
Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation" (ronpa).
Now, Stilwell is playing this game. There are n
verbal evidences, and Stilwell has m
"bullets". Stilwell will use these bullets to shoot every verbal evidence.
Verbal evidences will be described as some strings
Ai,
and bullets are some strings Bj.
The damage to verbal evidence Ai
from the bullet Bj
is f(Ai,Bj).
f(A,B)=∑i=1|A|?|B|+1[?A[i...i+|B|?1]=B?]
In other words, f(A,B)
is equal to the times that string B
appears as a substring in string A.
For example: f(ababa,ab)=2,
f(ccccc,cc)=4
Stilwell wants to calculate the total damage of each verbal evidence
Ai
after shooting all m
bullets Bj,
in other words is ∑mj=1f(Ai,Bj).
?
Input
The first line of the input contains a single number
T,
the number of test cases.
For each test case, the first line contains two integers
n,
m.
Next n
lines, each line contains a string Ai,
describing a verbal evidence.
Next m
lines, each line contains a string Bj,
describing a bullet.
T≤10
For each test case, n,m≤105,
1≤|Ai|,|Bj|≤104,
∑|Ai|≤105,
∑|Bj|≤105
For all test case, ∑|Ai|≤6?105,
∑|Bj|≤6?105,
Ai
and Bj
consist of only lowercase English letters
?
Output
For each test case, output
n
lines, each line contains a integer describing the total damage of
Ai
from all m
bullets, ∑mj=1f(Ai,Bj).
?
Sample Input
1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
?
Sample Output
1
1
0
3
7
?
Author
SXYZ
?
Source
2015 Multi-University Training Contest 8
解題:
??? 比賽的時(shí)候,看到那么多人過了,也想著套套AC自動(dòng)機(jī),但不會(huì)就是不會(huì)呀。沒有基礎(chǔ),賽后搜題解發(fā)現(xiàn)有人用字典樹過了,就學(xué)習(xí)一下。雖然感覺復(fù)雜度上可能會(huì)超,但學(xué)習(xí)一下字典樹也是不錯(cuò)的嘛!字典樹的查詢和構(gòu)建是唯一的難點(diǎn)。因?yàn)閯偨佑|還是不是很會(huì),就參考了這篇博客的做法。
??? 其實(shí)就是先將所有的B構(gòu)建成一顆字典樹,然后枚舉每個(gè)A,從A的不同起始位置開始去匹配B,查詢函數(shù)可以這樣理解。當(dāng)用A的一部分移動(dòng)到B的字典樹的某一節(jié)點(diǎn)時(shí),說明當(dāng)前往上的節(jié)點(diǎn)都已經(jīng)匹配成功了,那么也就是A中包含由B的根節(jié)點(diǎn)到當(dāng)前節(jié)點(diǎn)形成的路徑,那么加上該節(jié)點(diǎn)對(duì)應(yīng)的數(shù)量即可,一旦匹配失敗,那么再往下也不可能了,直接返回此時(shí)的數(shù)量就好了。
代碼:
#include#include#includeusing?namespace?std; struct?Trie { //存儲(chǔ)的節(jié)點(diǎn) int?ch[100010][26]; //val存儲(chǔ)的是節(jié)點(diǎn)權(quán)值,sz是節(jié)點(diǎn)數(shù)量 int?val[100010],sz; //初始化 void?init() { sz=1; memset(ch[0],0,sizeof(ch[0])); } //插入一條新的單詞記錄 void?insert(char?*s) { int?u=0,len=strlen(s); for(int?i=0;i<len;i++) { int?c=(s[i]-'a'); //如果該節(jié)點(diǎn)還不存在,創(chuàng)建該節(jié)點(diǎn) if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; //為該節(jié)點(diǎn)分配編號(hào) ch[u][c]=sz++; } //向下移 u=ch[u][c]; } val[u]++; } //查詢?cè)撚涗? int?query(char?*s) { int?len=strlen(s),u=0,c,res=0; for(int?i=0;i<len;i++) { ???c=s[i]-'a'; ???if(ch[u][c]) ???{ ???//其實(shí)因?yàn)閍的查詢是以a的起始位置不斷后移的 ???//這是從a往后不同長度匹配b ???u=ch[u][c]; ???res+=val[u]; ???} ???else? ???return?res; } return?res; } }; Trie?T; char?a[100005][10010]; char?ss[10010]; int?main() { ????int?n,m,t,len,ans; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); //初始化 T.init(); //先將a讀入并存下來 ????????for(int?i=0;i<n;i++) { getchar(); scanf("%s",a[i]); } //用b構(gòu)建字典樹 for(int?i=0;i<m;i++) { getchar(); scanf("%s",ss); T.insert(ss); } //以a的不同起始位置取查詢結(jié)果 for(int?i=0;i<n;i++) { len=strlen(a[i]); ans=0; for(int?j=0;j<len;j++) { ???????????????ans+=T.query(a[i]+j); } printf("%dn",ans); } } return?0; }