Educational Codeforces Round 12(C)贪心

C. Simple Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ababazscoder are simple whereas aaadd are not simple.

zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this task!

Input

The only line contains the string s (1 ≤ |s| ≤ 2·105) — the string given to zscoder. The string s consists of only lowercase English letters.

Output

Print the simple string s' — the string s after the minimal number of changes. If there are multiple solutions, you may output any of them.

Note that the string s' should also consist of only lowercase English letters.

Examples
input
aab
output
bab
input
caaab
output
cabab
input
zscoder
output
zscoder


题意:给你一个字符串,让你修改最少得次数,使得这个字符串相邻的字符不相同



题解:贪心就好啦, 使用前缀和扫出相同字符子串的最大长度(注意只有相同的字符才继承前面的值,否则就从1开始计数),然后每2个字符修改一次,因为一个字符最少的修改次数明显是(n/2)




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;

#define N int(1e6)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;

char s[N];
int pre[N];
vector<pair<int, int> >pos;
int main()
{
#ifdef CDZSC
	freopen("i.txt", "r", stdin);
	//freopen("o.txt","w",stdout);
	int _time_jc = clock();
#endif
	while (~scanf("%s", s))
	{
		pre[strlen(s)] = 1;
		pos.clear();
		pre[0] = 1;
		for (int i = 1; s[i]; i++)
		{
			pre[i] = 1;
			if (s[i] == s[i - 1])
			{
				pre[i] = pre[i - 1] + 1;
			}
		}
		for (int i = 0; s[i]; i++)
		{
			if (pre[i] > 1 && pre[i + 1] == 1)
			{

				pos.push_back(make_pair(pre[i], i));
			}

		}
		for (int i = 0; i < pos.size(); i++)
		{
			int x = pos[i].first;
			for (int j = pos[i].second - x + 2; j <= pos[i].second; j += 2)
			{
				char c = 'a';
				while (c == s[j + 1] || c == s[j - 1])
				{
					c++;
				}
				s[j] = c;
			}
		}
		printf("%s\n", s);
	}
	return 0;
}








你可能感兴趣的:(Educational Codeforces Round 12(C)贪心)