hdu5782

来源:互联网 发布:c语言中最大公约数 编辑:程序博客网 时间:2024/05/17 18:43
给定两个长度相等的字符串 
问他们的第 i个前缀是否循环相等 
循环相等的定义是,两个长度相等的字符串 

其中一个字符串能通过循环移位得到另一个

Bp[i][j]表示以B串中1~i为子串,和A中以j开头的子串是否匹配

#include <bitset>using namespace std;typedef pair<int,int> Pii;typedef long long LL;typedef unsigned long long ULL;typedef double DBL;typedef long double LDBL;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define SQR(a) ((a)*(a))#define PCUT puts("\n----------")const int maxn=1e4+10;//const int maxn=10;char A[maxn], B[maxn];bitset<maxn> posA[26], posB[26], Ap[maxn], Bp[maxn], ans, tem, pre[maxn];int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);    #endif    for(int i=1; i<maxn; i++) pre[i] = pre[i-1].set(i-1);    while(~scanf(" %s %s", A, B))    {        int N=strlen(A);        for(int i=0; i<26; i++) posA[i].reset(), posB[i].reset();        for(int i=0; i<N; i++)        {            posA[A[i]-'a'][i] = 1;            posB[B[i]-'a'][i] = 1;        }        Ap[0] = posB[A[0]-'a'];        Bp[0] = posA[B[0]-'a'];        for(int i=1; i<N; i++) Bp[i] = Bp[i-1] & (posA[B[i]-'a']>>i);        ans.reset(); tem.reset();        bool all = A[0]==B[0];        if(all) ans[0]=1;        for(int i=1; i<N; i++)        {            Ap[i] = Ap[i-1] & (posB[A[i]-'a']>>i);            int l=0, r=N-1;            while(l<r)            {                int mid=(l+r+1)>>1;                if(!Bp[mid][i]) r=mid-1;                else l=mid;            }            all &= A[i]==B[i];            if(all) ans[i]=1;            if(l==0 && A[i] != B[0]) continue;            ans |= (Ap[i-1]&pre[l+1]) << (i-1);        }        for(int i=0; i<N; i++) putchar(ans[i]+'0');        puts("");    }    return 0;}

0 0
原创粉丝点击