剪花布条Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20743 Accepted Submission(s): 12946
Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
Sample Output
Author
qianneng
Source
Recommend
|
//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
const int maxn=1e6+5;
const LL mod = 1e9+7;
const LL base = 1e9+13;
int cas=1;
LL Hash[maxn];
LL p[maxn]; //它会一直取mod, 保证它的范围在[0,2^64-1], 是负数也会自己取mod.
char s1[10005];
char s2[maxn];
void init(char *s)//预处理主串
{
p[0] = 1;
Hash[0] = 0;
int k =strlen(s+1);
for(int i=1;i<=k;i++) p[i] = p[i-1] * base % mod;
for(int i=1;i<=k;i++) Hash[i] = (Hash[i-1] * base % mod + (s[i] - 'A' + 1) % mod)%mod;
}
LL get(int l,int r)
{
return Hash[r]%mod - Hash[l-1] * p[r-l+1]%mod;
}
void solve()
{
while(~scanf("%s",s2+1))
{
//puts(s2+1);
if(s2[1] == '#') break;
scanf("%s",s1);
int len1 = strlen(s1);
int len2 = strlen(s2+1);
init(s2);
LL haha = 0;
int res = 0;
for(int i=0;i<len1;i++) haha = (haha * base %mod+ (s1[i] - 'A' + 1)%mod)%mod;
for(int i=1;i+len1-1<=len2;i+=len1)
{
LL hs = get(i,i+len1-1)%mod;
if(hs == haha)
res++;
}
printf("%d\n",res);
}
}
int main()
{
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}