【HDU 2087】剪花布条 【KMP or strstr函数】
来源:互联网 发布:python 矩阵相乘 编辑:程序博客网 时间:2024/05/21 10:45
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3
aaaaaa aa
#
Sample Output
0
3
代码一
#include<bits/stdc++.h>using namespace std ;typedef long long LL ; //调用strstr函数,不断查找const int MAXN = 1000+10 ;const int MAXM = 1e5 ;const int mod = 1e9+7 ;char a[MAXN],b[MAXN];int main(){ while(scanf("%s",&a)&&(a[0]!='#')){ scanf("%s",b); int len=strlen(b); int cnt=0; char *s=strstr(a,b); while(s!=NULL){ cnt++; s=strstr(s+len,b); } printf("%d\n",cnt); } return 0;}
代码二 KMP
#include<bits/stdc++.h>using namespace std ;typedef long long LL ;const int MAXN = 1e3+10 ;const int MAXM = 1e5 ;const int mod = 1e9+7;char s[MAXN],t[MAXN];int nexts[MAXN];void getnexts(){ int i,j; i=j=0; int m=strlen(t); nexts[0]=-1;j=-1; while(i<m){ if(j==-1||t[i]==t[j]) { nexts[++i]=++j; }else j=nexts[j]; }}void Kmp(){ int i,j;i=j=0; int n=strlen(s); int m=strlen(t); int ans=0; while(i<n){ if(j==-1||s[i]==t[j]) { ++i;++j; if(j==m) { ans++; j=0; } }else j=nexts[j]; } printf("%d\n",ans);}int main(){ while(scanf("%s",s)&&s[0]!='#'){ scanf("%s",t); getnexts(); //puts("========="); Kmp(); } return 0;}
阅读全文
0 0
- 【HDU 2087】剪花布条 【KMP or strstr函数】
- HDU-#2087 剪花布条(KMP & strstr)
- hdu 2087 剪花布条 strstr使用 或者 KMP
- HDOJ 2087 剪花布条----strstr函数
- HDU 2087 剪花布条 kmp
- HDU 2087 剪花布条(KMP)
- HDU 2087 剪花布条 KMP
- hdu 2087 剪花布条(KMP)
- HDU 2087 剪花布条 裸KMP
- HDU 2087 剪花布条 KMP
- hdu 2087 剪花布条(KMP)
- HDU 2087 剪花布条(KMP)
- HDU 2087 剪花布条(KMP:贪心)
- HDU-2087 剪花布条(kmp)
- HDU 2087 剪花布条 KMP入门
- HDU 2087 剪花布条 //简单kmp
- hdu 2087 剪花布条 KMP
- HDU 2087 剪花布条 KMP题解
- 获取设备的UDID用于给远程手机安装测试包
- 基本类型数组转换列表陷阱(asList)
- D
- jQuery常用方法
- tomcat中的server.xml文件配置了URIEncoding="UTF-8"需要注意的问题
- 【HDU 2087】剪花布条 【KMP or strstr函数】
- Tr A
- 【笔记篇】C#笔记3
- 字符串处理func_split,func_split_TotalLength
- TensorFlow从入门到实战资料汇总
- html获取url参数
- JavaScript sort() 方法详解
- DeepLearning Trick
- 启动WebLogic时提示:此时不应有\java\jdk1.8.0_144\lib\dt.jar;