HDU 2087 KMP求匹配串的重复次数
2016-07-19 03:09
274 查看
剪花布条
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
HDU
2087
Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3
aaaaaa aa
#
Sample Output
0
3
KMP求匹配串的重复次数
shift or :
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
HDU
2087
Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3
aaaaaa aa
#
Sample Output
0
3
KMP求匹配串的重复次数
#include <iostream> #include <map> #include <cstring> #include <set> #include <cstdio> #include <string> using namespace std; int nex[1005]; void pre(string str){ int j=-1; nex[0]=-1; for(int i=1;i<str.size();i++){ while(j>=0&&str[j+1]!=str[i]) j=nex[j]; if(str[j+1]==str[i]) j++; nex[i]=j; } } int kmp(string str1,string str2){ pre(str2); int ans=0; int j=-1; for(int i=0;i<str1.size();i++){ while(j>=0&&str2[j+1]!=str1[i]) j=nex[j]; if(str2[j+1]==str1[i]) j++; if(j==str2.size()-1){ ans++; j=-1;//找到归负一继续找 } } return ans; } int main() { string str1,str2; while(cin>>str1){ if(str1=="#") break; cin>>str2; cout<<kmp(str1,str2)<<endl; } return 0; }
shift or :
#include <bits/stdc++.h> using namespace std; const int MAXN=1010; bitset <MAXN> ch[129]; bitset <129> vis; bitset <MAXN> p; char str1[MAXN]; char str2[MAXN]; int main(){ while(scanf("%s",str1)){ if(strcmp(str1,"#")==0) return 0; scanf("%s",str2); for(int i=0;i<129;i++) ch[i].set(); int len1=strlen(str1),len2=strlen(str2); for(int i=0;i<len2;i++) ch[str2[i]].reset(i); p.set(); int ans=0; for(int i=0;i<len1;i++){ p=p<<1|ch[str1[i]]; if(p[len2-1]==0){ p.set(); ans++; } } printf("%d\n",ans); } }
相关文章推荐
- HDU 1711 KMP求匹配位置
- 发朋友圈的九条建议
- HDU 1686 求子串的数量
- 如何让文章像病毒一样传播
- Leetcode 307. Range Sum Query - Mutable (Python)
- office2016永久免费激活码(office2016密钥)
- “重定向次数过多”或者“Too many automatic redirections were attempted”的错误:
- nyoj 301 递推求值 矩阵快速幂
- 来选择一款适合我网站的CMS建站程序吧?
- XGen 苹果IOS神器一键新机改串清理超级全息备份支持IOS789超IGV8使用分享(企鹅290093670)
- 百度站长链接实时提交工具V1.0
- 统计学 回归分析( Regression Analysis)
- Amazing Slider(网页图片轮播制作)V6.2中文特别版
- 模拟链表
- HDU 1237 简单计算器
- C#接口
- ShareX(图片文件分享程序)V11.1.0中文版
- office 2010 密钥(office 2010永久的密钥)
- SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
- Interleaving String