Danganronpa
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 971 Accepted Submission(s): 522
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) .
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) .
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
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
就一模版题。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
#define T 100010
int kid[6*T][26],v[6*T],fail[6*T];
struct actrie
{
int L,root;
actrie(){
L = 1,root = 0;
memset(kid[0],-1,sizeof(kid[0]));
}
int newnode(){
v[L] = 0;
memset(kid[L],-1,sizeof(kid[L]));
return L++;
}
void build(){
queue<int> q;
for(int i=0;i<26;++i){
if(kid[0][i]==-1){
kid[0][i] = root;
} else {
fail[kid[0][i]] = root;
q.push(kid[0][i]);
}
}
while(!q.empty())
{
int p = q.front();q.pop();
for(int i=0;i<26;++i){
if(kid[p][i]==-1){
kid[p][i] = kid[fail[p]][i];
} else {
fail[kid[p][i]] = kid[fail[p]][i];
q.push(kid[p][i]);
}
}
}
}
void insert(string s){
int p = root;
for(int i=0;s[i];++i){
int d = s[i]-'a';
if(kid[p][d]==-1){
kid[p][d] = newnode();
}
p = kid[p][d];
}
v[p]++;
}
int query(string s)
{
int p = root;
int tmp,sum=0;
for(int i=0;s[i];++i){
int d = s[i]-'a';
tmp = p = kid[p][d];
while(tmp!=root)
{
sum+=v[tmp];
tmp = fail[tmp];
}
}
return sum;
}
};
string s[T],str;
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int N,n,m,i;
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&n,&m);
actrie ac;
for(i=0;i<n;++i){
cin >> s[i];
}
for(i=0;i<m;++i){
cin >> str;
ac.insert(str);
}
ac.build();
for(i=0;i<n;++i){
cout << ac.query(s[i])<<endl;
}
}
return 0;
}
网上的另外一种写法(感觉我的更好,嘻嘻):
这种将定义放在主函数外面的定义方式就能在结构体内开很大的数组了,一开始不知道,所以就感觉为什么别人代码能开,我不能开而困惑。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
#define T 100010
struct actrie
{
int kid[6*T][26],v[6*T],fail[6*T];
int L,root;
void Init(){
L = 1,root = 0;
memset(kid[0],-1,sizeof(kid[0]));
}
int newnode(){
v[L] = 0;
memset(kid[L],-1,sizeof(kid[L]));
return L++;
}
void build(){
queue<int> q;
for(int i=0;i<26;++i){
if(kid[0][i]==-1){
kid[0][i] = root;
} else {
fail[kid[0][i]] = root;
q.push(kid[0][i]);
}
}
while(!q.empty())
{
int p = q.front();q.pop();
for(int i=0;i<26;++i){
if(kid[p][i]==-1){
kid[p][i] = kid[fail[p]][i];
} else {
fail[kid[p][i]] = kid[fail[p]][i];
q.push(kid[p][i]);
}
}
}
}
void insert(string s){
int p = root;
for(int i=0;s[i];++i){
int d = s[i]-'a';
if(kid[p][d]==-1){
kid[p][d] = newnode();
}
p = kid[p][d];
}
v[p]++;
}
int query(string s)
{
int p = root;
int tmp,sum=0;
for(int i=0;s[i];++i){
int d = s[i]-'a';
tmp = p = kid[p][d];
while(tmp!=root)
{
sum+=v[tmp];
tmp = fail[tmp];
}
}
return sum;
}
};
string s[T],str;
actrie ac;
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int N,n,m,i;
scanf("%d",&N);
while(N--)
{
ac.Init();
scanf("%d%d",&n,&m);
for(i=0;i<n;++i){
cin >> s[i];
}
for(i=0;i<m;++i){
cin >> str;
ac.insert(str);
}
ac.build();
for(i=0;i<n;++i){
cout << ac.query(s[i])<<endl;
}
}
return 0;
}