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 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的更多相关文章
- hdu 2669 Romantic
Romantic Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- hdu 2669 Romantic (乘法逆元)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 『摄影欣赏』16幅 Romantic 风格照片欣赏【组图】
今天,我们将继续分享人类情感的系列文章.爱是人类最重要的感觉,也可能是各种形式的艺术(电影,音乐,书,画等)最常表达的主题 .这里有40个最美丽的爱的照片,将激励和给你一个全新的视觉角度为这种情绪.我 ...
- HDU 4901 The Romantic Hero
The Romantic Hero Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u D ...
- HDU4901 The Romantic Hero 计数DP
2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/30 ...
- UVA 1351 十三 String Compression
String Compression Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- NEU校园网登录器
http://www.cnblogs.com/weidiao/p/5124106.html 改自学长的博客. 我们的目标是写一个程序实现自动登录校园网.而这基于的是表单的post机制. 输入校园网网址 ...
- HDU 4901 The Romantic Hero (计数DP)
The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...
- 1351 topcoder 吃点心
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 先按low从大到小贪心再high从小到大贪心 #pragma c ...
随机推荐
- Proteus中的 PIC10/12/16 MCUs编译器无法下载的问题
当你打开网站时,点击该软件下载会发现如下页面: google一下会出现这个界面,大意是这个版本的编译器太老了,已经被某些更加高级的编译器给取代了(qaq心痛) 然后我就开始FQ到处google,Sou ...
- linux学习之路4 系统目录架构
linux树状文件系统结构 bin(binary) 保存可执行文件 也就是保存所有命令 boot 引导目录 保存所有跟系统有关的引导程序 其中Vmlinux文件最为重要,是系统内核 dev 保存所有的 ...
- FFT学习及简单应用(一点点详细)
什么是FFT 既然打开了这篇博客,大家肯定都已经对FFT(Fast Fourier Transformation)有一点点了解了吧 FFT即为快速傅里叶变换,可以快速求卷积(当然不止这一些应用,但是我 ...
- java 键盘输入多种方法
转! 分类: java学习2012-11-04 09:58 8427人阅读 评论(1) 收藏 举报 一.java不像C中拥有scanf这样功能强大的函数,大多是通过定义输入输出流对象.常用的类有Buf ...
- .Net MVC之间的关系以及如何运用
.Net MVC组成部分:视图(views)模型(model)控制器(controller)以及路由(RouteConfig),视图跟模型的数据不进行直接的交互,他们是通过控制器进行视图模型之间的数据 ...
- 关于defineProperty
var c=0; Object.defineProperty(this,'b',{ get :function(){ return ++c; } }) console.log(b===1&&a ...
- github与git常用的一些基本配置与命令
首次Git设置:ssh-keygen -t ras -C "email@xxx" 生成SSH (共钥)将生成的SSH key复制到文本框中即可(title默认为邮箱名) 你的身份( ...
- Ajax异步刷新省市级联
省市级联在web前端用户注册使用非常广泛.Ajax异步刷新省市级联.如图:选择不同的区,自动加载相应的街. <TD class=field>位 置:</TD> <TD&g ...
- vs2013打包
C#打包需要这个:InstallShield 2013 Limited Edition for Visual Studio . 下载地址: InstallShield 2013 Limited Ed ...
- 00Enterprise Resource Planning
Enterprise Resource Planning 企业资源计划即 ERP (Enterprise Resource Planning),由美国 Gartner Group 公司 ...