转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
题目:给出一个串S,一个串T。S中有多少个串可以通过“处理”得到串T。
处理是指,将串分成两部分,调换顺序
http://codeforces.com/contest/235/problem/C
CLJ出的题,TAT~~~~~~~~~~
主要是那步处理有点麻烦,不过将两个T串拼接在一起
那么T’中的所有子串便是T经过处理得到的。
将S串建立SAM,然后跑一遍T',匹配一下。
如果匹配长度大于|T|,则要判断一下,我们找到当前位置的某个后缀的最大匹配长度恰 好大于|T|。这点直接可以通过pre指针实现,pre指针所能接受的串是当前位置的后缀,还是满足的。
但是注意去重,T‘中的相同子串不能跑多遍,所以在SAM上加个标记
之前先拓扑预处理一下某结点到达终态的数目。
这是CLJ的官方题解
This problem can be solved by many suffix structures.
I think suffix automaton is the best way to solve it because it is simple and clear.
So let us build a suffix automaton of the input string S.
And consider the query string x.
let us build a string t to be x concatenate x and drop the last char.
so every consecutive sub-string of t with length |x| is a rotation of x.
let us read string t with suffix automaton we've build, and every time take the first char out and add a new char,add the answer by the number of string equal to this current sus-btring of t(which is a rotation of x).
And one more thing,we should consider the repetend of x as well,
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1600005
#define M 40
#define N 1100001
#define maxn 2000005
#define eps 1e-7
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define MOD 1000000007
#define lson step<<1
#define rson step<<1|1
#define sqr(a) ((a)*(a))
#define Key_value ch[ch[root][1]][0]
#define test puts("OK");
#define pi acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define vi vector<int>
using namespace std;
struct SAM{
SAM *pre,*son[26];
int len,cnt,mark;
}*root,*tail,que[N*2],*b[N*2];
int tot;
void add(int c,int l){
SAM *np=&que[tot++],*p=tail;
tail=np;np->len=l;
while(p&&p->son[c]==NULL) p->son[c]=np,p=p->pre;
if(p==NULL) np->pre=root;
else{
SAM *q=p->son[c];
if(p->len+1==q->len) np->pre=q;
else{
SAM *nq=&que[tot++];
*nq=*q;
nq->len=p->len+1;
np->pre=q->pre=nq;
while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre;
}
}
}
char str[N];
int cnt[N];
void slove(){
mem(cnt,0);
int l=strlen(str);
for(int i=0;i<tot;i++) cnt[que[i].len]++;
for(int i=1;i<=l;i++) cnt[i]+=cnt[i-1];
for(int i=0;i<tot;i++) b[--cnt[que[i].len]]=&que[i];
SAM *p=root;
for(int i=0;i<l;i++) (p=p->son[str[i]-'a'])->cnt++;
for(int i=tot-1;i>0;i--){
if(b[i]->pre) b[i]->pre->cnt+=b[i]->cnt;
}
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(scanf("%s",str)!=EOF){
tot=0;
root=tail=&que[tot++];
for(int i=0;str[i];i++)
add(str[i]-'a',i+1);
slove();root->cnt=0;
//for(int i=0;i<tot;i++) printf("%d\n",que[i].cnt);
int q,mark=0;
scanf("%d",&q);
while(q--){
mark++;
int ans=0;
SAM *p=root;
scanf("%s",str);
int l=strlen(str),len=0;
for(int i=0;i<2*l;i++){
int c=str[i<l?i:i-l]-'a';
if(p->son[c]) len++,p=p->son[c];
else{
while(p&&p->son[c]==NULL) p=p->pre;
if(p==NULL) len=0,p=root;
else len=p->len+1,p=p->son[c];
}
if(len>=l){
while(p->pre&&p->pre->len>=l) len=p->pre->len,p=p->pre;
if(p->mark!=mark){
p->mark=mark;
ans+=p->cnt;
}
}
}
printf("%d\n",ans);
}
for(int i=0;i<tot;i++){
mem(que[i].son,NULL);
que[i].pre=NULL;
que[i].mark=0;
}
}
return 0;
}