剪花布条
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11552 Accepted Submission(s): 7427
Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3 aaaaaa aa #
Sample Output
0 3
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&(-x))
#define PB push_back
#define For(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef vector<int> vint;
typedef vector<string> vstring;
void RI (int& x){
x = 0;
char c = getchar ();
while (c == ' '||c == '\n') c = getchar ();
bool flag = 1;
if (c == '-'){
flag = 0;
c = getchar ();
}
while (c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar ();
}
if (!flag) x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}
const double PI = acos(-1.0);
const int maxn = 1e3 + 100;
const int maxm = 1e3 + 100;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
const int INF = 0x7fffffff;
/************************************END DEFINE*********************************************/
string s,t;
int nxt[maxm];
void get_next(){
int len = t.size();
nxt[0] = 0;
nxt[1] = 0;
For(i,1,len){
int j = nxt[i];
while(j && t[i] != t[j])j = nxt[j];
nxt[i+1] = t[i] == t[j] ? j+1 : 0;
}
}
int kmp(){
get_next();
int j = 0,cnt = 0;
For(i,0,s.size()){
while(j && t[j] != s[i])j = nxt[j];
if(t[j] == s[i]) j++;
if(j == t.size()){
cnt ++;
j = 0;
}
}
return cnt;
}
int main()
{
while(cin>>s){
if(s == "#")break;
cin>>t;
int ans = kmp();
cout<<ans<<endl;
}
return 0;
}