原题链接

注意: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);
}
}

最新文章

  1. LeetCode &quot;468. Validate IP Address&quot;
  2. js中的定义
  3. Could not load file or assembly Microsoft.Web.Infrastructure
  4. WEB-INF目录下的文件访问权限(待解决)
  5. 使用 FileZilla FTP Client连接Vsftpd在执行LIST命令后提示连接超时
  6. 学习28个HTML5特征、窍门和技术
  7. ACM竞赛常用STL(一)
  8. 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标
  9. hibernate与mybatis的区别
  10. Linux 运行级别
  11. 元组tuple基本操作
  12. [BZOJ 3456]城市规划
  13. Redux进阶(一)
  14. codeforces463D
  15. RedHat Enterprise Linux 6.4使用网易Centos 6 的yum源
  16. python基础数据类型练习2
  17. QTP 学习 - 参数化
  18. P2569 [SCOI2010]股票交易
  19. Qt 添加 QtNetwork 库文件
  20. 九、将cs文件快速的转换成可执行文件和响应文件(配置编译开关的文件)

热门文章

  1. day7—直播内容(元昊老师著)
  2. Redis(四)-- 集群
  3. 第七篇:几个经典的TCP通信函数
  4. 小程序 - API 踩坑记录(更新中...)
  5. [干货] 有了微信小程序,谁还学ReactNative?
  6. Shell主要逻辑源码级分析 (2)——SHELL作业控制
  7. struts2 中redirectAction如何传递参数!
  8. JQuery 用法总结
  9. Charles(网络封包分析工具)
  10. less常见的操作