A题:Common Substrings(KMP应用)
2024-10-19 00:25:24
注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1e5+;
char ch[*maxn];
char s[maxn],t[maxn];
int T,next[*maxn];
/*1.
void get_next(char *s)
{
next[1]=0;
//printf("%d\n",next[1]);
int i=1,j=0;
int slen=strlen(s);
while(i<slen){
if(j==0||s[i-1]==s[j-1]){
i++,j++;
next[i]=j;
//printf("%d\n",next[i]);
}
else j=next[j];
}
}严蔚敏数据结构P83页代码
*/
void get_next(char *s)
{
int slen=strlen(s);
next[]=;
for(int i=;i<slen;i++){
int j=next[i];
while(j&&s[i]!=s[j]) j=next[j];
next[i+]=(s[i]==s[j])?j+:;
}
}
/*3.
void get_next(char *s)
{
next[0]=0,next[1]=0;
//printf("%d\n",next[1]);
int slen=strlen(s);
for(int i=2;i<=slen;i++){
int j=next[i-1];
while(j&&s[i-2]!=s[j-1]) j=next[j];
next[i]=(s[i-2]==s[j-1])?j+1:1;
//printf("%d\n",next[i]);
}
}按照做数据结构笔试题的步骤一步一步推导
*/
int main()
{
scanf("%d",&T);
while(T--){
cin>>s>>t;
int slen=strlen(s);
int tlen=strlen(t);
for(int i=;i<tlen;i++) ch[i]=t[i];
ch[tlen]='#';
for(int i=;i<slen+tlen+;i++) ch[i+tlen+]=s[i];
int now=slen+tlen+;
ch[now]=;
int ans=;
memset(next,,sizeof(next));
get_next(ch);
while(next[now]>){
ans++;
now=next[now];
}
printf("%d\n",ans);
}
}
最新文章
- LeetCode ";468. Validate IP Address";
- js中的定义
- Could not load file or assembly Microsoft.Web.Infrastructure
- WEB-INF目录下的文件访问权限(待解决)
- 使用 FileZilla FTP Client连接Vsftpd在执行LIST命令后提示连接超时
- 学习28个HTML5特征、窍门和技术
- ACM竞赛常用STL(一)
- 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标
- hibernate与mybatis的区别
- Linux 运行级别
- 元组tuple基本操作
- [BZOJ 3456]城市规划
- Redux进阶(一)
- codeforces463D
- RedHat Enterprise Linux 6.4使用网易Centos 6 的yum源
- python基础数据类型练习2
- QTP 学习 - 参数化
- P2569 [SCOI2010]股票交易
- Qt 添加 QtNetwork 库文件
- 九、将cs文件快速的转换成可执行文件和响应文件(配置编译开关的文件)