题目传送门

 1 /*
 2     题意:问一个串在另一个串出现的次数(可重复)
 3     KMP:模板题
 4 */
 5 /************************************************
 6 * Author        :Running_Time
 7 * Created Time  :2015-8-9 19:45:40
 8 * File Name     :POJ_3461.cpp
 9  ************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 1e6 + 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 int nex[MAXN];
37 char s[MAXN], t[MAXN];
38
39 void get_nex(int lm)    {
40     int i = 0, j = -1;   nex[0] = -1;
41     while (i < lm)  {
42         if (j == -1 || t[j] == t[i])    {
43             j++;    i++;    nex[i] = j;
44         }
45         else    j = nex[j];
46     }
47 }
48
49 int KMP(void)   {
50     int ln = strlen (s);    int lm = strlen (t);
51     get_nex (lm);
52     int i = 0, j = 0;   int ans = 0;
53     while (i < ln)  {
54         while (j != -1 && t[j] != s[i]) j = nex[j];
55         j++;    i++;
56         if (j >= lm) {
57             ans++;  j = nex[j];
58         }
59     }
60     return ans;
61 }
62
63 int main(void)    {     //POJ 3461 Oulipo
64     int T;  scanf ("%d", &T);
65     while (T--)   {
66         scanf ("%s%s", t, s);
67         printf ("%d\n", KMP ());
68     }
69
70     return 0;
71 }

 1 /*
 2     题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4194
 3      给你两个字符串A,B,请输出B字符串在A字符串中出现了几次(不可重复)
 4 */
 5 /************************************************
 6 * Author        :Running_Time
 7 * Created Time  :2015-8-9 19:45:40
 8 * File Name     :ZSTU_4194.cpp
 9  ************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 1e6 + 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 int nex[MAXN];
37 char s[MAXN], t[MAXN];
38
39 void get_nex(int lm)    {
40     int i = 0, j = -1;   nex[0] = -1;
41     while (i < lm)  {
42         if (j == -1 || t[j] == t[i])    {
43             i++;    j++;    nex[i] = j;
44         }
45         else    j = nex[j];
46     }
47 }
48
49 int KMP(void)   {
50     int ln = strlen (s);
51     int lm = strlen (t);
52     get_nex (lm);
53     int i = 0, j = 0;   int ans = 0;
54     while (i < ln)  {
55         while (j != -1 && s[i] != t[j]) j = nex[j];
56         i++;    j++;
57         if (j == lm) {
58             ans++;  j = 0;      //改动这里就是重新匹配
59         }
60     }
61     return ans;
62 }
63
64 int main(void)    {
65     while (scanf ("%s%s", s, t) == 2)   {
66         printf ("%d\n", KMP ());
67     }
68
69     return 0;
70 }
71
72 不可重复的匹配

不可重复的匹配

转载于:https://www.cnblogs.com/Running-Time/p/4717802.html

KMP POJ 3461 Oulipo相关推荐

  1. poj 3461 Oulipo (KMP)

    http://poj.org/problem?id=3461 基础KMP, 要注意一次查找完成后,到下一可查找处继续匹配,这样才能保证得到最终个数. code: #include<cstdio& ...

  2. poj 3461 Oulipo(kmp统计子串出现次数)

    题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...

  3. POJ 3461 Oulipo(kmp算法解析)

    题目链接:https://cn.vjudge.net/contest/320014#problem/F Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERD ...

  4. poj 3461 - Oulipo

    KMP算法,按书上说的写一遍,总是很别扭,后来才知道是数组开始问题,就是从"1"还是从"0"开始,废了很多脑力,又增几多白发,才把书上的从1开始改为从0开始.昨 ...

  5. POJ 3461 Oulipo

    KMP模板题 #include<iostream> #include<cstring> #include<cstdio> using namespace std; ...

  6. POJ - 3461 (kmp)

    题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  7. POJ 3461 字符串匹配(KMP / 哈希(有推导))

    文章目录 1. 题目 1.1 题目链接 1.2 题目大意 2. Accepted代码 2.1 KMP解法 2.2 哈希法(有推导过程) 1. 题目 1.1 题目链接 http://poj.org/pr ...

  8. Oulipo POJ - 3461【KMP】

    这道题说了很多,但其实就是给了你两个字符串p和t,输出p在t中出现的次数 在KMP模板上的改动是 if(j==len1){ // printf("i=%d j=%d\n",i,j) ...

  9. POJ 3461 KMP

    题意:       给你两个字符串,问你串a在串b里面出现了多少次,可以重叠, 比如aaa aaaaa 是3 ,不是1. 思路:       就是在KMP的时候当匹配到比配串的最后一位的时候sum + ...

最新文章

  1. C#与RSS亲密接触
  2. Oracle wrap 和 unwrap( 加密与解密) 说明
  3. python第三方包安装方法(两种方法)
  4. Java虚拟机——Java内存区域与内存溢出
  5. 抢票 | AI未来说学术论坛第八期 深度学习特别专场
  6. 现代软件工程 作业 原型设计
  7. JavaScript Swiper插件
  8. SQLAlchemy文档翻译
  9. 开始学习 refactoring:improving the design of existing code
  10. python画图怎么确定坐标_像素坐标与绘图坐标
  11. 机器学习项目实战——集成预测政治献金
  12. Extracting Relational Facts by an End-to-End Neural Model with Copy Mechanism
  13. DOS、DOS攻击、DDOS攻击、DRDOS攻击
  14. HTML网页设计制作——初音动漫(6页) dreamweaver作业静态HTML网页设计模板
  15. 如何获取微软官方原版Windows 10 ISO镜像文件
  16. 游戏开发之Unity学习(五)——鼠标打飞碟(Hit UFO)
  17. 剑网三重置版服务器维护,《剑网3:指尖江湖》新人须知,端游重制版是道伤疤,请勿论...
  18. XamlParseException异常
  19. QML类型:ApplicationWindow
  20. [Win+Python]使用python接口测试时302重定向的两种处理方法

热门文章

  1. Oracle创建简单视图案例
  2. spring-boot 中实现标准 redis 分布式锁
  3. jQuery EasyUI 选项卡面板tabs使用实例精讲
  4. html css双色径向渐变,CSS 径向渐变
  5. 下载python需要注意什么_用户在对Python下载的时候,这些注意事项不能忽视
  6. VS Code设置代码片段(C++)
  7. 【结合实例】信息增益的计算
  8. 详细推导PCA算法(包括算法推导必备的知识)
  9. 安卓canvas设置HTML,安卓开发中view和canvas的理解
  10. 2.2.2 定点数的运算(移位、原码和补码的加减乘除、溢出概念和判别方法)