一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?

Input输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input

abcde a3
aaaaaa  aa
#

Sample Output

0
3
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

const int N = 1000002;
int nextt[N];
char S[N],T[N];
//vector<int>S,T;
int slen, tlen;

void getNext()
{
    int j, k;
    j = 0; k = -1; nextt[0] = -1;
    while(j < tlen)
        if(k == -1 || T[j] == T[k])
            nextt[++j] = ++k;
        else
            k = nextt[k];

}
/*
返回模式串T在主串S中首次出现的位置
返回的位置是从0开始的。
*/
int KMP_Index()
{
    int i = 0, j = 0,ans=0;
    getNext();

    while(i < slen && j < tlen)
    {
        if(j == -1 || S[i] == T[j])
        {
            i++; j++;
        }
        else
            j = nextt[j];
		if(j == tlen){
			ans++;
			j=0;
		}
    }
    return ans;
}

int main()
{
    
    int TT;
    int i, cc;
    while(1)
    {
    	scanf("%s", S);
    	if(S[0]=='#'&&strlen(S)==1)break;
    	scanf("%s", T);
    	
        slen = strlen(S);
        tlen = strlen(T);
        cout<<KMP_Index()<<endl;
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注