天天看點

HDU 2087 剪花布條

Problem Description

一塊花布條,裡面有些圖案,另有一塊直接可用的小飾條,裡面也有一些圖案。對于給定的花布條和小飾條,計算一下能從花布條中盡可能剪出幾塊小飾條來呢?

Input

輸入中含有一些資料,分别是成對出現的花布條和小飾條,其布條都是用可見ASCII字元表示的,可見的ASCII字元有多少個,布條的花紋也有多少種花樣。花紋條和小飾條不會超過1000個字元長。如果遇見#字元,則不再進行工作。

Output

輸出能從花紋布中剪出的最多小飾條個數,如果一塊都沒有,那就老老實實輸出0,每個結果之間應換行。

Sample Input

abcde a3

aaaaaa aa

#

Sample Output

3

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 500005;
int nt[maxn], tot, x;
char s[maxn], S[maxn];

int main()
{
  while (scanf("%s", S) && S[0] != '#')
  {
    scanf("%s", s);
    nt[0] = x = -1;
    for (int i = tot = 0; s[i]; i++)
    {
      int k = nt[i];
      while (k >= 0 && s[i] != s[k]) k = nt[k];
      nt[i + 1] = k + 1;
    }
    for (int i = 0, j = nt[1]; S[i];)
    {
      if (j < 0 || S[i] == s[j])
      {
        i++, j++;
        if (!s[j] && i - j > x){ x = i - 1; tot++; }
      }
      else j = nt[j];
    }
    printf("%d\n", tot);
  }
  return 0;
}