LIGHTOJ 1255-SUBSTRING FREQUENCY 【KMP】
2015-06-05 23:24
591 查看
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1255
题意:求母串中包含子串的个数,可重叠。
代码:
题意:求母串中包含子串的个数,可重叠。
代码:
[code]#include <stdio.h> #include <iostream> #include <math.h> #include <stdlib.h> #include <ctype.h> #include <algorithm> #include <vector> #include <string.h> #include <queue> #include <stack> #include <set> #include <map> #include <sstream> #include <time.h> #include <malloc.h> using namespace std; void get_next(char x[], int m, int Next[]) { int i, j; j = Next[0] = -1; i = 0; while (i < m) { while (-1 != j && x[i] != x[j]) j = Next[j]; Next[++i] = ++j; } } int Next[1001000]; long long KMP(char x[], int m, char y[], int n)//x模式串 y主串 { int i, j; long long ans = 0; i = j = 0; get_next(x, m, Next); while (i < n) { while (-1 != j && y[i] != x[j]) j = Next[j]; i++; j++; if (j >= m) { ans++; j = Next[j]; } } return ans; } char a[1000100], b[1000100]; int main() { int t, num; scanf("%d", &t); for (int i = 1; i <= t; i++) { printf("Case %d: ", i); scanf("%s", a); int n = strlen(a); scanf("%s", b); int m = strlen(b); printf("%lld\n", KMP(b, m, a, n)); } }
相关文章推荐
- iOS Programming: The Big Nerd Ranch Guide (4th Edition) 阅读笔记-关于View和View Hierarchy
- OpenCSP开源程序解析之OPENCSP_AuthUI.cpp
- JavaWeb系列之七(response域与request)
- UITableViewCell重用和性能优化
- iOS学习笔记(2)— UIView用户事件响应
- org.json.JSONException: No value for xxx
- UIScrollView
- 内核中的中断函数request_irq()
- quick接入SDK
- [GUI] QT事件与X11的关系
- UINavigationController 导航控制器
- leetcode_PermutationSequence
- ActionScript 3.0 学习(二) Flash Builder找不到所需版本的Adobe Flash Player
- NGUI使用4——创建动态字体
- NGUI使用3——制作图集&自定义按钮&切换按钮图片
- NGUI使用2——Button -- 按钮
- NGUI使用1——label
- UI进阶 —— 收起键盘方法
- UIGestureRecognizer 六种手势的用法 - iOS - UI基础知识总结6
- 一次性移除一个 UIView 所有子视图的方法