思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降……

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define llson j<<1,l,mid
#define rrson j<<1|1,mid+1,r
#define seed 131
#define INF 0x7fffffff
#define maxn 200105
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ull base[maxn],hash1[maxn],hash2[maxn];
int n,m;
char s1[maxn],s2[maxn];
int judge(int i,int j)
{
int l=0,r=m,mid,ans=0;
while(l<=r)
{
mid=(l+r)>>1;//二分最长公共前缀
if(i+mid-1>n||j+mid-1>m)
{
r=mid-1;
continue;
}
ull a=hash1[i+mid-1]-hash1[i-1]*base[mid];
ull b=hash2[j+mid-1]-hash2[j-1]*base[mid];
if(a==b) l=mid+1,ans=mid;
else r=mid-1;
}
return ans;
}
int main()
{
freopen("1.txt","r",stdin);
int i,t,ii=1,flag;
scanf("%d",&t);
for(i=1,base[0]=1;i<maxn;i++)
base[i]=base[i-1]*seed;
while(t--)
{
scanf("%s%s",s1,s2);
n=strlen(s1),m=strlen(s2);
for(i=1,hash1[0]=0;i<=n;i++)
hash1[i]=hash1[i-1]*seed+s1[i-1]-'a'+1;
for(i=1,hash2[0]=0;i<=m;i++)
hash2[i]=hash2[i-1]*seed+s2[i-1]-'a'+1;
for(i=1;i<=n-m+1;i++)//枚举第一个字符串的起始位置
{
int j=1,k=i,cnt=0;
flag=0;
while(k<=n)
{
int len=judge(k,j);//两个位置的最长公共前缀
k+=len+1;
j+=len+1;
cnt++;
if(cnt==2)
{
if(j>m||j+judge(k,j)>m) flag=1;
break;
}
if(j>m) {flag=1;break;}
}
if(flag) break;
}
if(flag) printf("Case #%d: %d\n",ii++,i-1);
else printf("Case #%d: -1\n",ii++);
}
return 0;
}

BNU 34990 Justice String (hash+二分求LCP)的更多相关文章

  1. BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34990 DEBUG了非常久,还是legal的推断函数写错了... 此题做法.枚举Stri ...

  2. BNUOJ 34990 Justice String

    Justice String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...

  3. acdream1116 Gao the string!(hash二分 or 后缀数组)

    问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...

  4. 后缀数组 hash求LCP BZOJ 4310: 跳蚤

    后缀数组的题博客里没放进去过..所以挖了一题写写 充实下博客 顺便留作板子.. 一个字符串S中 内容不同的子串 有 sigma{n-sa[i]+1-h[i]}   (噢 这里的h[]就是大家熟知的he ...

  5. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

  6. bnuoj 34990(后缀数组 或 hash+二分)

    后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀. 我自己在想hash的时候一直在考虑hash成数值时MOD ...

  7. 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...

  8. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

  9. LOJ#6387 「THUPC2018」绿绿与串串 / String (Manacher || hash+二分)

    题目描述 绿绿和 Yazid 是好朋友.他们在一起做串串游戏. 我们定义翻转的操作:把一个串以最后一个字符作对称轴进行翻转复制.形式化地描述就是,如果他翻转的串为 RRR,那么他会将前 ∣R∣−1个字 ...

随机推荐

  1. logrotate 学习使用

    logrotate Summary : Rotates, compresses, removes and mails system log files Description : The logrot ...

  2. Android 显示或隐藏标题栏进度条TitleProgressBar

    1.新建项目,布局文件如下:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/ ...

  3. ubuntu 安装TensorFlow

    1.安装pip $ sudo apt-get install python-pip python-dev 2.安装 TensorFlow for Python 2.7 # Ubuntu/Linux - ...

  4. Codeforces Round 480 Div 2 光荣掉分记

    痛 痛苦 痛苦啊. 越接近黄名想的越多了啊…… 都说了不要在意rating这破玩意了…… 没出E就算了,策略问题. 居然还FST了: FST个D就算了: FST个A算个**啊. 紧张的时候总会写出一些 ...

  5. HashSet底层存储元素的源码分析

    此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持.它不保证 set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用 null 元素. HashSet<Strin ...

  6. System.getProperty("os.name")

    Here is a handy Java class that useSystem.getProperty("os.name") to detect which type of o ...

  7. 两个imageView实现图片轮播

    前言 在不少的项目中,都会用到图片轮播这个功能,现在网上关于图片轮播的轮子也层出不穷,千奇百怪,笔者根据自己的思路,用两个imageView也实现了图片轮播,这里给大家介绍笔者的主要思路以及大概步骤. ...

  8. 【OpenJudge3531】【背包DP】【膜法交配律】判断整除

    判断整除 总时间限制: 1000ms 内存限制: 65536kB [描述] 一个给定的正整数序列,在每个数之前都插入+号或-号后计算它们的和.比如序列:1.2.4共有8种可能的序列:(+1) + (+ ...

  9. Spring项目搭建

    1,新建web项目 2,导入jar包 3,编写配置文件 <?xml version="1.0" encoding="UTF-8"?> <bea ...

  10. 【mybatis】mybatis中的<if test=“”>test中多条件

    mybatis中的<if test=“”>test中多条件 代码展示: 其中 accountCode和apiName都是ApiAllRespBean的属性 <select id=&q ...