【题目链接】

点击打开链接

【算法】

KMP

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXW 10010
#define MAXT 1000010 int T,l1,l2;
char s1[MAXW],s2[MAXT];
int next[MAXW]; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void getnext() {
int i,pos;
next[] = ;
for (i = ; i <= l1; i++) {
pos = next[i-];
while (pos > && s1[pos+] != s1[i]) pos = next[pos];
if (s1[pos+] == s1[i]) next[i] = pos + ;
else next[i] = ;
}
} inline void kmp() {
int i,pos=,sum=;
for (i = ; i <= l2; i++) {
while (pos > && s2[i] != s1[pos+]) pos = next[pos];
if (s2[i] == s1[pos+]) pos++;
else pos = ;
if (pos == l1) sum++;
}
writeln(sum);
} int main() { read(T);
while (T--) {
scanf("%s%s",s1+,s2+);
l1 = strlen(s1+); l2 = strlen(s2+);
getnext();
kmp();
} return ; }

最新文章

  1. 理解Docker(3):Docker 使用 Linux namespace 隔离容器的运行环境
  2. [原创]直播服务器简单实现 http_flv和hls 内网直播桌面
  3. 子句jion
  4. C#:WebBrowser控件设置代理IP访问网站【附源码】
  5. 2014 Super Training #7 C Diablo III --背包问题(DP)
  6. 浅析ado.net获取数据库元数据信息 DeriveParameters
  7. bzoj3721 [PA2014 Final] Bazarek
  8. linux下执行strlwr函数出错:ld returned 1 exit status
  9. H2最完整的资料下载地址:
  10. 利用angular控制元素的显示和隐藏
  11. JavaScript: 使用 atan2 来绘制 箭头 和 曲线
  12. 安装selenium
  13. Django---手动编写视图
  14. 配置Nim的默认编译参数 release build并运行
  15. 深入理解java虚拟机(二)-----垃圾回收
  16. C# Winfrom MDI(多文档界面)
  17. array扩展运算符
  18. sqlserver创建计算列 转
  19. ASP.NET 实现多页面合并一页显示
  20. 基于Kafka+Spark Streaming+HBase实时点击流案例

热门文章

  1. Codeforces 123 E Maze
  2. BZOJ3674 可持久化并査集
  3. error MIDL2311 解决方法
  4. DELPHI方法注释的标准写法
  5. People seldom do what they believe in. They do what is convenient, then repent.
  6. 利用反射技术实现POJO的数据库操作
  7. 【Java编程】建立一个简单的JDBC连接-Drivers, Connection, Statement and PreparedStatement
  8. 重新认识一遍JavaScript
  9. python—networkx:依据图的权重绘图
  10. poj 2154 Color 欧拉函数优化的ploya计数