题目描述

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. hihocoder #1698 假期计划 (排列组合+费马小定理+乘法逆元)

    Description 小Ho未来有一个为期N天的假期,他计划在假期中看A部电影,刷B道编程题.为了劳逸结合,他决定先拿出若干天看电影,再拿出若干天刷题,最后再留若干天看电影.(若干代指大于0)  每 ...

  2. ACM_N皇后问题

    N皇后问题 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不 ...

  3. Github 文件选择性上传

    用过Github的人都知道.gitignore文件的存在,但是实际用起来还是有一些需要注意的地方,尤其是对于新手来说,稍不注意就会出错.   一.Github选择性忽略特定文件的方式 1.全局设置 一 ...

  4. 专题五:TCP编程

    前言 前面专题的例子都是基于应用层上的HTTP协议的介绍, 现在本专题来介绍下传输层协议——TCP协议,主要介绍下TCP协议的工作过程和基于TCP协议的一个简单的通信程序,下面就开始本专题的正文了. ...

  5. [ TJOI 2010 ] 打扫房间

    \(\\\) Description 给出一个\(N\times M\) 的网格,一些格子是污点,求是否能用多个封闭的环路覆盖所有不是污点的格点. 封闭的环路覆盖的含义是,每条路径都必须是一个环,且每 ...

  6. Centos 开机自动联网

    默认情况下,Centos不是自动连接上网的,要点击右上角有个电脑图标,选择system eth0进行连接, 可以修改开机启动配置只需修改:/etc/sysconfig/network-scripts/ ...

  7. RPC——笔记

    整理的笔记来源:https://mp.weixin.qq.com/s/JkXrPcuKtE2qYgmDcH2uww RPC(远程过程调用): RPC是:一个计算机通信协议. 调用过程:计算机 A 上的 ...

  8. mongo 3.4分片集群系列之四:搭建分片集群--哈希分片 + 安全 + 区域

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  9. Angular——单页面与路由的使用

    单页面 SPA(Single Page Application)指的是通单一页面展示所有功能,通过Ajax动态获取数据然后进行实时渲染,结合CSS3动画模仿原生App交互,然后再进行打包(使用工具把W ...

  10. dubbo之服务容器

    服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...