题目描述

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 1575: [Usaco2009 Jan]气象牛Baric【dp】

    完了不会dp了 设f[i][j]为以i结尾,有j个时的最优值,辅助数组g[i][j]为s选了i和j,i~j中的误差值 转移是f[j][i]=min(f[k][i-1]+g[k][j]) #includ ...

  2. 观光公交 2011年NOIP全国联赛提高组(贪心,递推)

    观光公交 2011年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 风景迷人的小城 Y 市 ...

  3. 微信小程序去除button边框

    button::after{ border: none; } 去button的背景

  4. php+ajax+jquery实现jsonp跨域

    我们有这么个html文件test.html: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  5. 常用JavaScript代码库(又名:WFang.js)

    1.根据公司项目封装ajax请求,结合layer框架一起使用 /*提取接口公共部分*/ var ApiConf = { server:"http://localhost:8080/Batte ...

  6. 239 Sliding Window Maximum 滑动窗口最大值

    给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...

  7. Lind.DDD.DynamicModules动态模块化的设计

    回到目录 在Lind.DDD框架里有Module,主要用于全局自动添加的模块,它类似于ABP系统里的Module,但有时过于自动化了可能使系统太死板,而有时将需要的模块手动载入可能对我们更合适,所以大 ...

  8. P3373 【模板】线段树 2 区间求和 区间乘 区间加

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别 ...

  9. java 8 stream使用

    使用stream代替循环的方案 1.定义一个Article类包括标题.作者.标签 private class Article { private final String title; private ...

  10. 三维重建:GitHub百度Apollo 2.0

    GitHub:https://github.com/ApolloAuto/apollo 1. 关于Apollo的数据:Apollo的数据会如何开放? 自动驾驶数据将包括具有高分辨率图像和像素级别标注的 ...