HDU - 2087 剪花布条

HDU - 2087
Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

 Status

Description

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

Input

输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。 
 

Output

输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。 
 

Sample Input

abcde a3 aaaaaa aa #
 

Sample Output

0 3
 

Source

冬练三九之二
思路:KMP,在一个母串中快速匹配子串,该题所做的改变就是在匹配过程中,当满足条件并不退出,而是继续匹配,看母串中究竟能够产生多少个子串。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include<cstdlib>
#include <cmath>
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
#define maxn 1000500
int next_[maxn];
char str1[maxn];
char str2[maxn];
int len_1,len_2;
int conut;
void get_next()
{
    int i=0;
    int j=-1;
    next_[i]=j;
    while(i<len_2)
    {
        if(j==-1 || str2[i]==str2[j])
        {
            ++i;
            ++j;
            next_[i]=j;
        }
        else
            j=next_[j];

    }
}
void kmp()
{
    get_next();
    int i=0;
    int j=0;
    int len=len_1;
    while(i<len)
    {
        if(j==-1 || str1[i]==str2[j])
        {
            ++i;
            ++j;
        }
        else
            j=next_[j];
        if(j==len_2)
        {
            ++conut;
            j=0;
        }

    }

}
int main()
{
    while(~scanf("%s",str1) && str1[0]!='#')
    {
        memset(next_,-1,sizeof(next_));
        conut=0;
        scanf("%s",str2);
        len_1=strlen(str1);
        len_2=strlen(str2);
        kmp();
        cout<<conut<<endl;
    }
    return 0;
}


posted on 2016-07-12 10:36  胖胖的乓乓  阅读(178)  评论(0编辑  收藏  举报

导航