【HihoCoder - 1850】字母去重 (字符串,思维)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84258220

题干:

给定一个字符串S,每次操作你可以将其中任意一个字符修改成其他任意字符。

请你计算最少需要多少次操作,才能使得S中不存在两个相邻的相同字符。

Input

只包含小写字母的字符串S。  

1 ≤ |S| ≤ 100000

Output

一个整数代表答案

Sample Input

aab

Sample Output

1

解题报告:

   考虑对于s[i]字符,如果改变,则会影响到s[i-1] 和 s[i+1],所以我们考虑aab和aaa的情况。不难发现我们改变中间那个字符为任意值就可以了。。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int main()
{
	int ans = 0;
	cin>>s;
	int len = strlen(s);
	for(int i = 0; i<len; i++) {
		if(s[i] == s[i+1]) {
			s[i+1] ='@';ans++;
		}
		
	} 
	printf("%d\n",ans);

	return 0 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84258220