【Codeforces Round 855】D. Remove Two Letters

题目链接

Problem - D - Codeforces

题目翻译

输入格式

输出格式

解题思路

观察测试用例aaabcc我们不难发现,当删除[0, 1]时剩下的s[2]为a, 当删除s[1, 2]时剩下的s[0]为a,此时剩余的两个字符串完全相等。当删除s[2, 3]时剩下的s[4]为c,当删除s[3, 4]时剩下的s[2]为a,此时剩余的两个字符串不相等。

因此我们的解题思路为:设置cnt为总共不相同的字符串数目,枚举判断s[i]和s[i + 2]是否相等,如果相等,则说明删除s[i, i + 1]得到的字符串和删除s[i + 1, i + 2]得到的字符串完全一样,此时cnt--,表示有两个字符串相同。

代码实现

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10, M = 2 * N;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int hash_num = 131;
const double eps = 1e-6;
const double pi = acos(-1);
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long double LD;


int main()
{	
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n;
		scanf("%d", &n);
		string s;
		cin >> s;
		int cnt = n - 1;
		for(int i = 0; i < n - 2; i ++) if(s[i] == s[i + 2]) cnt --;
		printf("%d\n", cnt);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值