C. Good String Codeforces Round #560 (Div. 3)

本文详细解析了C.GoodString问题,探讨了如何通过最少的字符删除使字符串符合特定条件,即长度为偶数且奇数位置字符不等于其后的偶数位置字符。文章提供了完整的代码实现及样例解释,帮助读者理解并解决此类字符串操作问题。

C. Good String

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, stringand xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.

You are given a string ss, you have to delete minimum number of characters from this string so that it becomes good.

Input

The first line contains one integer n (1≤n≤2⋅1051≤n≤2⋅105) — the number of characters in s.

The second line contains the string s, consisting of exactly n lowercase Latin letters.

Output

In the first line, print one integer k (0≤k≤n0≤k≤n) — the minimum number of characters you have to delete from s to make it good.

In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.

Examples

input

Copy

4
good

output

Copy

0
good

input

Copy

4
aabc

output

Copy

2
ab

input

Copy

3
aaa

output

Copy

3

 

 

题意:题目中规定合格的字符串的标准:1,长度为偶数 2,字符串奇数位和下一位即偶数位数字不能相等。求在原字符串中最少删除几个字符使其成为符合要求的字符串。

 

我的代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stack>

using namespace std;
stack<char>s;
char a[400005];
stack<char>ch;

void prtstk(){
	while(!s.empty()){
		ch.push(s.top());
		s.pop();
	}
	while(!ch.empty()){
		cout<<ch.top();
		s.push(ch.top());
		ch.pop();
	}
	cout<<endl;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		while(!s.empty())
		{
			s.pop(); 
		}
		while(!ch.empty())
		{
			ch.pop(); 
		}
		scanf("%s",a);
		int len = strlen(a);
		for(int i=0;i<len;i++)
		{
			if(i == 0)
			{
				s.push(a[i]);
			}
			else
			{
				if(s.size()%2==0)
				{
					s.push(a[i]);
				}
				else
				{
					if(s.top()!=a[i])
					{
						s.push(a[i]);
					}
				}
			}
		}
		if(s.size()%2==1)
		{
			printf("%d\n",n-s.size()+1);
			s.pop();
			if(s.size()!=0)
			{
				prtstk();
			}
		}
		else
		{
			printf("%d\n",n-s.size());
			if(s.size()!=0)
			{
				prtstk(); 
			}
		}
	}
	return 0;
}

 

容易错的样例:

输入

4

gdoo

5

aaaaa

5

aabba

输出

gd

            (为空串)

abba

### Codeforces Round 1013 Div. 3 部分题目解析 #### A. Good Numbers 此题的核心在于统计输入数据中特定数字的频率并验证其是否满足条件。具体实现方法如下: 程序通过 `map` 数据结构存储每个数字出现的次数,并逐一检查这些计数值是否达到指定的要求。 ```cpp void solve() { int n, m, k; cin >> n; map<int, int> mp; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { mp[a[i]]++; if (mp[2] >= 2 && mp[0] >= 3 && mp[1] >= 1 && mp[3] >= 1 && mp[5] >= 1) { cout << i << endl; return; } } cout << 0 << endl; } ``` 上述代码逻辑清晰地展示了如何利用哈希表来解决该问题[^1]。 --- #### B. Team Training 对于本题,目标是从给定字符串中删除某些字符以形成子串 `"2020"`。解决方案采用枚举的方式尝试不同的分割位置组合,从而快速判断是否存在合法方案。 以下是完整的 C++ 实现代码片段: ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define endl &#39;\n&#39; void solve() { int n; string a; cin >> n >> a; for (int i = 0; i <= 4; i++) { if (a.substr(0, i) + a.substr(n - (4 - i), 4 - i) == "2020") { puts("YES"); return; } } puts("NO"); } signed main() { int _ = 1; cin >> _; while (_--) solve(); } ``` 这段代码实现了对所有可能情况的有效覆盖,并提供了高效的解答方式。 --- #### 更多题目概述 虽然未提供其他题目的详细描述,但可以根据比赛惯例推测其余部分涉及的内容范围广泛,涵盖了动态规划、贪心算法以及图论等领域知识点。有兴趣者可访问官方平台获取完整版题解资料[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值