HDU—— 2087 剪花布条
来源:互联网 发布:耐思尼克域名注册平台 编辑:程序博客网 时间:2024/06/06 16:52
题意:中文自己看。
解题思路:利用KMP就能解决问题,详见代码,如有看不懂的,请看另外一篇关于KMP的博文。
Code:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn = 1010;int f[maxn];void getFail(char *P)//失配函数{ int m = strlen(P); f[0] = 0, f[1] = 0; for(int i = 1; i < m; i++) { int j = f[i]; while(j && P[i] != P[j]) j = f[j]; f[i+1]=P[i]==P[j] ? j+1:0; }}int find(char *P, char *T){ int n = strlen(T), m = strlen(P); getFail(P); int j = 0, num = 0; for(int i = 0; i < n;) { while(j && P[j] != T[i]) j = f[j]; if(P[j] == T[i]) j++; if(j == m) { ++num; j = 0;}//num用来统计在T中有多少个P,每次得到一个P之后,j要置0 i++; } return num;//返回统计的P的个数}int main(){ //freopen("input.txt","r",stdin); char P[maxn],T[maxn]; while(scanf("%s",T)) { int len1 = strlen(T); if(len1 == 1 && T[0] == '#') break; scanf("%s",P); int temp = find(P,T); printf("%d\n",temp); } return 0;}
0 0
- HDU—— 2087 剪花布条
- HDU——2087 剪花布条
- HDU——2087剪花布条
- HDU 2087 剪花布条
- HDU 2087 剪花布条
- Hdu 2087 - 剪花布条
- hdu 2087 剪花布条
- HDU 2087 剪花布条
- hdu-2087 剪花布条
- HDU 2087 剪花布条
- hdu 2087 剪花布条
- HDU 2087 剪花布条
- hdu 2087 剪花布条
- HDU 2087 剪花布条
- hdu 2087 剪花布条
- HDU 2087 剪花布条
- hdu 2087 剪花布条
- hdu 2087 剪花布条
- printf 各种参数总结
- thymeleaf模板引擎和shiro框架的整合
- hibernate学习笔记
- c++知识点之 函数指针
- ORA-00937
- HDU—— 2087 剪花布条
- Android数据库高手秘籍(六)——LitePal的修改和删除操作
- Leetcode-Swap Nodes in Pairs
- xcode armv6 armv7 armv7s arm64
- 第17章 程序管理与SELinux初探
- 自己写了个coffeescript操作mongoose的demo
- #ifdef __cplusplus extern "C" { #endif 的解释
- 高级宏
- java.lang.NoClassDefFoundError: org/apache/commons/pool/ObjectPool