【HihoCoder - 1850】字母去重 (字符串,思维)
生活随笔
收集整理的這篇文章主要介紹了
【HihoCoder - 1850】字母去重 (字符串,思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:
給定一個字符串S,每次操作你可以將其中任意一個字符修改成其他任意字符。
請你計算最少需要多少次操作,才能使得S中不存在兩個相鄰的相同字符。
Input
只包含小寫字母的字符串S。 ?
1 ≤ |S| ≤ 100000
Output
一個整數代表答案
Sample Input
aabSample 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 ;}?
總結
以上是生活随笔為你收集整理的【HihoCoder - 1850】字母去重 (字符串,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10年前,用100万在深圳买房和买茅台的
- 下一篇: rsmsink.exe - rsmsin