Oulipo

传送门

代码:

#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 N 1000010
using namespace std;
char s[N], t[N];
int next[N];
void getnext(int l)//求模式串的next数组
{
	next[0] = -1;
	next[1] = 0;
	int i = 1, k = 0;
	while (i < l) {
		if (k < 0 || s[i] == s[k])
			next[++i] = ++k;
		else if (k >= 0 && s[i] != s[k])
			k = next[k];
	}
}
int Search(int l1,int l2){
	int ans = 0;
		int i = 0, k = 0;
		while (i < l2)//利用next数组,减少重复匹配
		{
			if (k < 0 || t[i] == s[k])//匹配上了,两者都 +1
				i++, k++;
			else //失配的话,更新 k
				k = next[k];
			if (k == l1)//完全匹配
			{
				ans++;//次数 +1 
				k = next[k];  //当作失配处理
			}
		}
		return ans;
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--){
		scanf("%s %s", s, t);
		int l1 = strlen(s);
		int l2 = strlen(t);
		getnext(l1);
		int ans=Search(l1,l2);
		printf("%d\n", ans);
	}
}

2.14

代码:

#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;
ll GCD(ll a, ll b) {//最大公约数
	return b == 0 ? a : GCD(b, a % b);
}
ll Power(ll a, ll b, ll p) { //计算(a^b)%p;
	ll ans = 1;
	while (b) {
		if (b & 1) //等价于b%2,判断b的奇偶性
			ans = ans * a % p; //如果为奇数,证明该位为1,需要乘上a
		a = a * a % p; //计算a^(2^i)
		b >>= 1; //等价于b/=2;
	}
	return ans;
}
bool isprime(int num)
{
	if (num == 2 || num == 3)
		return true;
	if (num % 6 != 1 && num % 6 != 5)
		return false;
	int t = sqrt((double)num);
	for (int i = 5; i <= t; i += 6)
	{
		if (num % i == 0 || num % (i + 2) == 0)
			return false;
	}
	return true;
}
ll flag[20000];
void GetFlag(string s) {
	ll len = s.length();
	flag[0] = -1;
	flag[1] = 0;
	ll i = 1, k = 0;
	while (i < len) {
		if (k < 0 || s[i] == s[k]) {
			flag[++i] = ++k;
		}
		else {
			k = flag[k];
		}
	}
}
ll Sum(string s1, string s2) {
	ll len1 = s1.length(), len2 = s2.length(),sum=0;
	ll i = 0, j = 0;
	while (i < len1) {
		if (j < 0 || s1[i] == s2[j]) {
			++i;
			++j;
		}
		else {
			j = flag[j];
		}
		if (j == len2) {
			sum++;
			j = flag[j];
		}
	}
	return sum;
}
int main() {
	FAST_IO;
	ll n, m, i, j, k, t;
	string s1, s2;
	while (cin >> k) {
		while (k--) {
			cin >> s2 >> s1;
			mes(flag, 0);
			GetFlag(s2);
			ll sum = Sum(s1, s2);
			cout << sum << endl;
		}
	}
}

2020.8.12

#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[10010];
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 || s1[i] == s2[k])i++, k++;
		else if (s1[i] != s2[k]) k = kmp[k];
		if (k == s2.length())sum++, k = kmp[k];
	}
	cout << sum << endl;
}
int main() {
	FAST_IO;
	while (cin >> k) {
		while (k--) {
			cin >> s2 >> s1;
			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、付费专栏及课程。

余额充值