题目描述

Do you know goagain? If the answer is “no”, well, you can leave NEUACM. Goagain is the most perfect ACMer in NEUACM, and his team’s 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 = +;
 6 typedef unsigned long long LL;
 7 const int x= ;
 8 LL H[MAX],xp[MAX];
 9 int id[MAX];
 LL hash[MAX];
 int n; char str[MAX];
  
 int cmp(int a,int b)
 {
     if(hash[a]!=hash[b]) return hash[a]<hash[b];
     else return a<b;
 }
  
 int check(int L)
 {
     int cnt=;
     for(int i=;i<n-L+;i++)
     {
         id[i]=i;
         hash[i]=H[i]-H[i+L]*xp[L];
     }
     sort(id,id+n-L+,cmp);
     for(int i=;i<n-L+;i++)
     {
         if(i==||hash[id[i]]!=hash[id[i-]]) cnt=;
         if(++cnt>=)return ;
     }
     return ;
 }
  
 int main()
 {
     //freopen("1.txt","r",stdin);
     while(scanf("%s",str)==)
     {
         n=strlen(str);
         H[n]=;
         for(int i=n-;i>=;i--) H[i]=H[i+]*x+str[i]-'a';
         xp[]=;
         for(int i=;i<=n;i++) xp[i]=xp[i-]*x;
         if(!check()) printf("0\n");
         else
         {
             int L=,R=n+; int ans;
             while(L<=R)
             {
                 int mid=(R+L)>>;
                 if(check(mid)) ans=mid,L=mid+;
                 else R=mid-;
             }
             printf("%d\n",ans);
         }
     }
     return 0

NEU 1351 Goagain and xiaodao's romantic story I的更多相关文章

  1. hdu 2669 Romantic

    Romantic Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  2. hdu 2669 Romantic (乘法逆元)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. 『摄影欣赏』16幅 Romantic 风格照片欣赏【组图】

    今天,我们将继续分享人类情感的系列文章.爱是人类最重要的感觉,也可能是各种形式的艺术(电影,音乐,书,画等)最常表达的主题 .这里有40个最美丽的爱的照片,将激励和给你一个全新的视觉角度为这种情绪.我 ...

  4. HDU 4901 The Romantic Hero

    The Romantic Hero Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u D ...

  5. HDU4901 The Romantic Hero 计数DP

    2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/30 ...

  6. UVA 1351 十三 String Compression

    String Compression Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  7. NEU校园网登录器

    http://www.cnblogs.com/weidiao/p/5124106.html 改自学长的博客. 我们的目标是写一个程序实现自动登录校园网.而这基于的是表单的post机制. 输入校园网网址 ...

  8. HDU 4901 The Romantic Hero (计数DP)

    The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...

  9. 1351 topcoder 吃点心

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 先按low从大到小贪心再high从小到大贪心 #pragma c ...

随机推荐

  1. bzoj 4596: [Shoi2016]黑暗前的幻想乡【容斥原理+矩阵树定理】

    真是简单粗暴 把矩阵树定理的运算当成黑箱好了反正我不会 这样我们就可以在O(n^3)的时间内算出一个无向图的生成树个数了 然后题目要求每个工程队选一条路,这里可以考虑容斥原理:全选的方案数-不选工程队 ...

  2. 测试神器Swagger的相关使用

    1.Swagger简介 swagger官网地址: https://swagger.io/ swagger官网文档介绍地址: https://swagger.io/about/ ​ swagge是一个易 ...

  3. 51NOD 1088 最长回文子串&1089 最长回文子串 V2(Manacher算法)

    回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度. Input 输入Str(Str的长度 <= 1000(第二题要 ...

  4. 22 C#中的异常处理入门 try catch throw

    软件运行过程中,如果出现了软件正常运行不应该出现的情况,软件就出现了异常.这时候我们需要去处理这些异常.或者让程序终止,避免出现更严重的错误.或者提示用户进行某些更改让程序可以继续运行下去. C#编程 ...

  5. php循环结构

    1.while循环 先判断条件,如果条件成立则执行循环的代码 嵌套循环,需要先把嵌套在内的循环执行完毕再执行外面的循环 While(条件语句){......} //如果()条件成立,执行{}里面的语句 ...

  6. TypeError: slice indices must be integers or None or have an __index__ method

    由于除法/自动产生的类型是浮点型,因此出现上述错误,修正方法为,将/更改为// roi_gray_lwpCV = gray_lwpCV[y:y + h // 2, x:x + w] # 检出人脸区域后 ...

  7. springboot运行模式

    1.springboot项目常见的运行方式:  2.说明: idea:在开发环境中跑项目,也就是我们在编码过程中的用的做多的方式 jar.war:线上.服务器上执行jar.war包的方式 maven插 ...

  8. dede手机访问网站跳转到手机端模板

    如何手机访问的时候跳转到自己的手机端模板,这时候需要一个js跳转代码:当手机访问的时候直接跳转到手机端 那手机端前提要有手机端的模板 <script> if(navigator.platf ...

  9. 【C++】智能指针简述(四):shared_ptr

    在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...

  10. vue2.0框架认识

    虚拟dom和声明式渲染: Vue的编译器在编译模板之后,会把这些模板编译成一个渲染函数 .而函数被调用的时候就会渲染并且返回一个 虚拟DOM的树 .这个树非常轻量,它的职责就是描述当前界面所应处的状态 ...