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
比赛没有看这道题,看题解知道是一道很裸的AC自动机,研究了好久,还没有想明白原理,但模板会套用了。
队友的AC自动机代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
#define N 100010
#define M 26
#define X 10010
char sentencea[N][X];
char worda[X];
struct AC{
int nexta[N][M];
int faila[N];
int enda[N];
int L;
int root;
int newnode(){
for(int i=0;i<M;i++){
nexta[L][i]=-1;
}
enda[L++]=0;
return L-1;
}
void init(){
L=0;
root=newnode();
}
void assert(char word[]){
int now=root;
for(int i=0;word[i]!='\0';i++){
int tempch=word[i]-'a';
if(nexta[now][tempch]==-1){
nexta[now][tempch]=newnode();
}
now=nexta[now][tempch];
}
enda[now]++;
}
void build(){
queue<int> q;
for(int i=0;i<M;i++){
if(nexta[root][i]==-1){
nexta[root][i]=root;//nexta[root][i]都不存在了,哪还有什么root一直到nexta[root][i]都相等这一说,所以当然不存在fail[nexta[root][i]]?乖,回去重新比较!
}
else{
faila[nexta[root][i]]=root;//这里nexta[root][i]已经非-1了,不需要重新赋值,但是由于nexta[root][i]是存在的所以存在一种可能从root一直到nexta[root][i]一直相等,所以好吧,给你个fail[nexta[root][i]]
q.push(nexta[root][i]);
}
}
while(!q.empty()){
int temp=q.front();
q.pop();
for(int i=0;i<M;i++){
if(nexta[temp][i]==-1){
nexta[temp][i]=nexta[faila[temp]][i];
}
else{
faila[nexta[temp][i]]=nexta[faila[temp]][i];
q.push(nexta[temp][i]);
}
}
}
}
long long int query(char sentence[]){
int now=root;
long long int ans=0;
for(int i=0;sentence[i]!='\0';i++){
now=nexta[now][sentence[i]-'a'];
int temp=now;
while(temp!=root){
ans+=(long long int)enda[temp];
temp=faila[temp];
}
}
return ans;
}
};
AC ac;
int main(){
int t;
int n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
ac.init();
for(int i=0;i<n;i++){
scanf("%s",sentencea[i]);
}
for(int i=0;i<m;i++){
scanf("%s",worda);
ac.assert(worda);
}
ac.build();
for(int i=0;i<n;i++){
printf("%lld\n",ac.query(sentencea[i]));
}
}
return 0;
}
看网上题解还有字典树解法
本题要求查找所有的B[j]在A[i]中出现的总次数。那么我们可以建立一颗字典树,将所有的B[j]插入字典树,由于一个串的所有字串相当于它所有后缀的前缀。因此在查找时候,只需要查找A[i]的每一个后缀即可,然后累加这个后缀的前缀个数,即可得到该后缀中子串的个数,所有后缀的值相加,就是最终的答案。
代码
#include<iostream>
#include<algorithm>
#include<cassert>
#include<string>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair <int, int> P;
const int N=100000+10;
int n,m;
char A[N][10005];
char B[N];
struct Trie
{
int tree[N][26],val[N],cnt;
Trie()
{
init();
}
void init()
{
cnt=1;
memset(tree,0,sizeof(tree));
}
void insert(char*s)
{
int p=0,l=strlen(s);
for(int i=0;i<l;i++)
{
int a=s[i]-'a';
if(!tree[p][a])
{
val[cnt]=0;
tree[p][a]=cnt++;
}
p=tree[p][a];
}
++val[p];
}
int query(char*s)
{
int p=0,l=strlen(s),ans=0;
for(int i=0;i<l;i++)
{
int a=s[i]-'a';
if(!tree[p][a])return ans;
p=tree[p][a];
ans+=val[p];
}
return ans;
}
}T;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
T.init();
for(int i=0;i<n;i++)
scanf("%s",A[i]);
ll ans;
for(int i=0;i<m;i++)
{
scanf("%s",B);
T.insert(B);//将所有的B[j]插入字典树
}
for(int i=0;i<n;i++)
{
ans=0;
int l=strlen(A[i]);
for(int j=0;j<l;j++)
ans+=T.query(A[i]+j);//枚举后缀的前缀在字典树中出现多少次,得到该后缀的返回值,累加就是答案
printf("%I64d\n",ans);
}
}
}