2021-03-23 每日两题

C. Canine poetry

题意:为了救出自己的爱人,丈夫必须要使三头恶犬沉睡。三头恶犬不喜欢回文字符串,丈夫可以对自己原有的字符串进行修改,使他不包含回文字符串。
一个大字符串是由许多个小的回文串组成,两种情况xx或xyx;
因为尾部的字符能和后面的字符匹配,可能成为回文串,所以我们优先改尾部的字符。

#include <bits/stdc++.h>

using namespace std;

int t;
string s;

int main()
{
	cin >> t;
	while (t -- )
	{
		cin >> s;
		int cnt = 0;
		for (int i = 0; i < s.size(); i ++ )
		{
			if (s[i] == '#') continue;
			if (s[i] == s[i + 1] && i + 1 < s.size())
			{
				cnt ++;
				s[i + 1] = '#';
			}
			if (s[i] == s[i + 2] && i + 2 < s.size())
			{
				cnt ++;
				s[i + 2] = '#';
			}
		}
		cout << cnt << endl;
	}
	
	return 0;
}

KMP打卡

#include <bits/stdc++.h>
using namespace std;

const int N = 100010, M = 1000010;

int n, m;
int ne[N];
char s[M], p[N];

int main()
{
    cin >> n >> p + 1 >> m >> s + 1;
    for (int i = 2, j = 0; i <= n; i ++ )
    {
        while (j && p[i] != p[j + 1]) j = ne[j];
        if (p[i] == p[j + 1]) j ++;
        ne[i] = j;
    }
    for (int i = 1, j = 0; i <= m; i ++ )
    {
        while (j && s[i] != p[j + 1]) j = ne[j];
        if (s[i] == p[j + 1]) j ++;
        if (j == n)
        {
            printf("%d ", i - n);
            j = ne[j];
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值