• NEU 1351 Goagain and xiaodao's romantic story I


    题目描述


    Do you know goagain? If the answer is no, well, you can leave NEUACM. Goagain is the most perfect ACMer in NEUACM, and his teams name is NEU_First Final. Besides this, goagain is also a Gao-Fu-Shuai. One day he falls in love a Bai-Fu-Mei, named xiaodao, another perfect ACMer in HIT. Though they only come across once, goagain has xiaodao daily and nightly in his thought. Finally goagain decides to express his love to xiaodao.After a long time, xiaodao replys to goagain that he needs to experience a test.

    There is a string S =[ s1,s2,s3,s4,s5,......sn ], and then m queries. Goagain s task is to calculate the longest prefix between suffix(a) and suffix(b) (1<=a,b<=L(s),a!=b)

    For example , string  A=[ a1,a2,a3,a4,.....an ].

    Suffix(i)= [ ai,ai+1,ai+2,ai+3,.....an ].

    Prefix of a string is [ a1,a2,a3,a4.....aj ] j<=n 

    输入

    There are several cases. For each case , the first line is string s(1<=L(s)<=100000).

    输出

    For each case, output the length of the longest prefix .

    样例输入

    aabaaaab 

    样例输出

    3

    题意:求出所有后缀中任意两个串的最长前缀。

    sl: 很显然的做法是后缀数组。。。本菜鸟不会,只好拿字符串hash恶搞。

     满足hash函数 hash[i][L]=H[i]-H[i+L]*xp[L];

    H[i]=H[i+1]*x+s[i]-'a'; 

    x为一个素数。

    然后可以二分最长前缀,看看是不是有两个串的hash值相同就行。

    由于是随机算法,一次傻逼可以重新换个x.

      1 #include<cstdio>

     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int MAX = 100000+10;
     6 typedef unsigned long long LL;
     7 const int x= 123;
     8 LL H[MAX],xp[MAX];
     9 int id[MAX];
    10 LL hash[MAX];
    11 int n; char str[MAX];
    12  
    13 int cmp(int a,int b)
    14 {
    15     if(hash[a]!=hash[b]) return hash[a]<hash[b];
    16     else return a<b;
    17 }
    18  
    19 int check(int L)
    20 {
    21     int cnt=0;
    22     for(int i=0;i<n-L+1;i++)
    23     {
    24         id[i]=i;
    25         hash[i]=H[i]-H[i+L]*xp[L];
    26     }
    27     sort(id,id+n-L+1,cmp);
    28     for(int i=0;i<n-L+1;i++)
    29     {
    30         if(i==0||hash[id[i]]!=hash[id[i-1]]) cnt=0;
    31         if(++cnt>=2)return 1;
    32     }
    33     return 0;
    34 }
    35  
    36 int main()
    37 {
    38     //freopen("1.txt","r",stdin);
    39     while(scanf("%s",str)==1)
    40     {
    41         n=strlen(str);
    42         H[n]=0;
    43         for(int i=n-1;i>=0;i--) H[i]=H[i+1]*x+str[i]-'a';
    44         xp[0]=1;
    45         for(int i=1;i<=n;i++) xp[i]=xp[i-1]*x;
    46         if(!check(1)) printf("0 ");
    47         else
    48         {
    49             int L=1,R=n+1int ans;
    50             while(L<=R)
    51             {
    52                 int mid=(R+L)>>1;
    53                 if(check(mid)) ans=mid,L=mid+1;
    54                 else R=mid-1;
    55             }
    56             printf("%d ",ans);
    57         }
    58     }
    59     return 0
  • 相关阅读:
    编写 grunt 插件经验
    Sencha Touch 手机移动开发框架 HTML5 项目压缩方案;
    随笔 编辑推荐 上头条了, 贴出来做个记念!
    Javascript 俄罗斯方块 游戏代码解释!
    30天自制操作系统(NASM+GCC版)
    Logisim 打不开的解决方案(Windows10)
    Kali Linux 2020通过UEFI硬盘安装(免u盘)
    开源一个自制的ORM框架,基于Java原生JDBC(应该是全网首个吧)
    书单
    前端技术文章收集
  • 原文地址:https://www.cnblogs.com/acvc/p/3702416.html
Copyright © 2020-2023  润新知