HDU 2087 剪花布条

剪花布条

Time Limit: 1000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38679
Accepted Submission(s): 23618

Problem Description

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

Input

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

Output

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

Sample Input

abcde a3
aaaaaa aa

Sample Output

0
3

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

vector<ll>v;
void KMP(string s) {
	v.clear();
	ll i = 0, j = 0, len = s.length(), k = 0;
	v.push_back(0);
	for (i = 1,j = 0; i < len; i++) {
		if (s[i] == s[j]) {
			j++;
			v.push_back(j);
		}
		else {
			if (j == 0)v.push_back(0);
			else j = 0,i--;
		}
	}
}//做下标

ll flag(string s1, string s2) {
	ll flag = 0;
	for (ll i = 0, j = 0, w = s1.length(), k = s2.length(); i < w; i++) {
		if (s1[i] == s2[j]) {
			j++;
			if (j == k) {
				j = 0;
				flag++;
			}
		}
		else {
			if (j != 0) {
				i--;
				j = v[j];
			}
		}
	}
	return flag;
}//查找
int main(){
	string s1, s2;
	ll i, j;
	while (cin >> s1) {
		if (s1 == "#")break;
		cin >> s2;
		if (s2 == "#")break;
		KMP(s2);
		cout << flag(s1, s2) << endl;
	}
}

优化版KMP:

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
ll n, m, i, j, k, t, flag, x, y, z;
const ll INF = 5e9 + 30;
string s1,s2;
ll kmp[1040];
void KMP() {
	kmp[0] = -1;kmp[1] = 0;
	ll i = 1, k = 0, j = s2.length();
	while (i < j) {
		if (k < 0 || s2[i] == s2[k])kmp[++i] = ++k;
		else k = kmp[k];
	}
}
void Find() {
	KMP();
	ll i = 0, k = 0, j = s1.length(), sum = 0;
	while (i < j) {
		if (k < 0) k = 0, i++;
		else if (s1[i] == s2[k])i++, k++;
		else if (s1[i] != s2[k]) k = kmp[k];
		if (k == s2.length())sum++, k = 0;
	}
	cout << sum << endl;
}
int main() {
	FAST_IO;
	while (cin >> s1 && s1 != "#" && cin >> s2) {
		Find();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GUESSERR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值