题目描述

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. 10.19NOIP模拟赛(DAY2)

    /* 正解O(n)尺取法orz 我写的二分答案.本来以为会被卡成暴力分...... 这个-'0'-48是我写的吗........我怎么不记得... */ #include<bits\stdc++ ...

  2. robotframework - 框架做接口自动化post请求

    1.做get请求之前先安装 Request库,参考github上链接 :https://github.com/bulkan/robotframework-requests/#readme 2.请求&a ...

  3. Unity项目 - Boids集群模拟算法

    1987年Craig W.Reynolds发表一篇名为<鸟群.牧群.鱼群:分布式行为模式>的论文,描述了一种非常简单的.以面向对象思维模拟群体类行为的方法,称之为 Boids ,Boids ...

  4. javascript实现继承的4种方法,以及它们的优缺点

    1. 原型链继承(有缺陷): 缺陷1:切断了Zi.prototype.constructor与Zi的关系 缺陷2:原型链上的引用类型的数据会被所有实例共享 2. 构造函数继承(有缺陷): 缺陷1:Fu ...

  5. spring cloud config搭建说明例子(二)-添加eureka

    添加注册eureka 服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</ ...

  6. QuartzJobs 如何发布服务

    http://www.cnblogs.com/jys509/p/4614975.html http://www.cnblogs.com/lc-chenlong/p/3948760.html 安装:To ...

  7. JQ 获取Table的td 值

    <script type="text/javascript"> function SetTable() { $("#myTab table").ea ...

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  9. NPOI 导出excel数据超65535自动分表

    工作上遇到的问题,网上找了一些资料 整理了一个比较可行的解决方案. NPOI 大数据量分多个sheet导出 代码段 /// <summary> /// DataTable转换成Excel文 ...

  10. 6.12---bug