zoukankan      html  css  js  c++  java
  • hdu 5782(kmp+hash)

    Cycle

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 677    Accepted Submission(s): 245


    Problem Description
    Alice get two strings and the lengths are both N. Bored Alice wanna know whether all equal length prefix string of these two strings are CycleEqual. For example, "abc" and "bca" and "cab" are CycleEqual. So you need output N character, '0' is no, '1' is yes.
    Input
    The input contains multiple test cases.
    For each test case, the first contains one string and the second line contains one string. The lengths of strings are both N(1N10000).
    Output
    For each test case, output N characters. For ith character, if prefix strings of first i character are CycleEqual, output '1', else output '0'.
    Sample Input
    abc cab
    aa aa
    Sample Output
    001
    11
    Author
    ZSTU
    Source
    题意:给你两个长度相等的字符串,问他们的所有的前缀能否构成循环同构
    假如str1,str2循环同构 则 str1=u+v  str2=v+u;
    其实我们只有在kmp的匹配过程中  每匹配一次(S[i]==T[j+1])成功的时候我们判断
    可以用字符串hash判断是否相等
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cctype>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<map>
     8 #include<queue>
     9 #include<stack>
    10 #include<set>
    11 #include<vector>
    12 #include<algorithm>
    13 #include<string.h>
    14 typedef long long ll;
    15 typedef unsigned long long LL;
    16 using namespace std;
    17 const int INF=0x3f3f3f3f;
    18 const double eps=0.0000000001;
    19 const int N=30000+10;
    20 const ll mod=1e9+7;
    21 const LL base=37;
    22 LL p[N];
    23 LL Hash[10][N];
    24 char a[N];
    25 char b[N];
    26 int Next[N];
    27 int ans[N];
    28 void init(){
    29     p[0]=1;
    30     for(int i=1;i<=10000;i++)p[i]=p[i-1]*base;
    31 }
    32 LL get_val(int l,int r,int t){
    33     LL ans=Hash[t][r]-Hash[t][l-1]*p[r-l+1];
    34     return ans;
    35 }
    36 int check(int i,int j,int t){
    37     i++;
    38     j++;
    39     if(i==j)return 1;
    40     if(get_val(j+1,i,t^1)==get_val(1,i-j,t))return 1;
    41     return 0;
    42 }
    43 void get_next(char *T){
    44     int len=strlen(T);
    45     Next[0]=-1;
    46     int j=-1;
    47     for(int i=1;i<len;i++){
    48         while(j!=-1&&T[j+1]!=T[i])j=Next[j];
    49         if(T[j+1]==T[i])j++;
    50         Next[i]=j;
    51     }
    52 }
    53 void kmp(char *S,char *T,int t){
    54     int lens=strlen(S);
    55     int lent=strlen(T);
    56     get_next(T);
    57    // for(int i=0;i<lent;i++)cout<<Next[i]<<" ";
    58     //cout<<endl;
    59     int j=-1;
    60     for(int i=0;i<lens;i++){
    61         while(j!=-1&&T[j+1]!=S[i])j=Next[j];
    62         if(S[i]==T[j+1]){
    63             j++;
    64             //cout<<i<<" "<<j<<endl;
    65             if(ans[i]==0){
    66                 ans[i]=check(i,j,t);
    67             }
    68         }
    69     }
    70 }
    71 int main(){
    72     init();
    73     while(scanf("%s%s",a+1,b+1)!=EOF){
    74         memset(ans,0,sizeof(ans));
    75         //cout<<a+1<<" "<<b+1<<endl;
    76         int lena=strlen(a+1);
    77         int lenb=strlen(b+1);
    78         for(int i=1;i<=lena;i++){
    79             Hash[1][i]=Hash[1][i-1]*base+a[i]-'a';
    80         }
    81         for(int i=1;i<=lenb;i++){
    82             Hash[0][i]=Hash[0][i-1]*base+b[i]-'a';
    83         }
    84         kmp(a+1,b+1,1);
    85         kmp(b+1,a+1,0);
    86         for(int i=0;i<lena;i++){
    87             cout<<ans[i];
    88         }cout<<endl;
    89     }
    90 }
     
  • 相关阅读:
    遍历文件夹与文件
    HMM&CRF
    获取js代码运行的时间
    小程序canvas绘制纯色圆角区域 setdata数组某一项
    layui type:2 iframe子页面向父页面传值
    layui 时间插件,change&&done,按照官网写法无效,解决方式!
    doT.js模板用法
    css画叉叉(一般用于关闭按钮)
    js复制文本到剪贴板
    node之module与fs文件系统
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/8604736.html
Copyright © 2011-2022 走看看