首页 技术 正文
技术 2022年11月16日
0 收藏 919 点赞 4,950 浏览 1158 个字

传送门:Justice String

题意:有两个串A,B,问是否存在A的一个子串S,S和B的长度相等,最多有2个字符不同。如果有多个,输出其实下标最小S的下标,没有输出-1。

分析:从A每个位置开始找最长公共前缀,如果最长公共前缀长度不大于lenb,继续从下一次位置开始找,至多找两次,如果一直找不到就算不存在。

#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL long long
#define N 100010
using namespace std;
const LL mod=1000000007ll;
const LL SEED=;
char a[N],b[N];
int lena,lenb;
unsigned LL hha[N],hhb[N];
unsigned LL p[N];
void init()
{
hha[]=a[];
hhb[]=b[];
for(int i=;i<lena;i++)
{
hha[i]=hha[i-]*SEED+a[i];
}
for(int i=;i<lenb;i++)
{
hhb[i]=hhb[i-]*SEED+b[i];
}
}
LL calc(int x,int len,unsigned LL f[])
{
return f[x+len-]-f[x-]*p[len];
}
int lcp(int pa,int pb)
{
int l=,r=lenb-pb,ans;
while(l<=r)
{
int m=(l+r)>>;
if(calc(pa,m,hha)==calc(pb,m,hhb))
l=m+,ans=m;
else r=m-;
}
return ans;
}
int main()
{
int T,cas=;
p[]=1ll;
for(int i=;i<N;i++)p[i]=p[i-]*SEED;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",a,b);
lena=strlen(a);
lenb=strlen(b);
init();
int ans=-;
for(int i=;i<=lena-lenb;i++)
{
int num=;
num+=lcp(i+num,num);
if(num>=lenb)
{
ans=i;break;
}
num++;
if(num>=lenb)
{
ans=i;break;
}
num+=lcp(i+num,num);
if(num>=lenb)
{
ans=i;break;
}
num++;
if(num>=lenb)
{
ans=i;break;
}
num+=lcp(i+num,num);
if(num>=lenb)
{
ans=i;break;
}
}
printf("Case #%d: %d\n",cas++,ans);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900