AtCoder Grand Contest 016(A - Shrinking)

A - Shrinking

 

题意:每次字符串长度减一,每个字符可以变成后一个字符或者保存不变,求最少操作多少次使得字符串都是一个相同的字符组成。

数字很小就直接枚举了。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 char s[110], ss[110];
 5 bool check(int x){
 6     for(int i = 0; i < strlen(ss)-x-1; i ++){
 7         if(ss[i] != ss[i+1])return false;
 8     }
 9     return true;
10 }
11 int fun(int x){
12     strcpy(ss,s);
13     int cnt = 0;
14     int len = strlen(ss);
15     while(1){
16         if(check(cnt))break;
17         for(int i = 0; i <len-cnt-1; i ++){
18             if(ss[i+1] == ('a'+x)){
19                 ss[i] = 'a'+x;
20             }else ss[i] = ss[i];
21         }
22         cnt++;
23     }
24     return cnt;
25 }
26 
27 int main(){
28     cin >> s;
29     int ans = strlen(s)/2;
30     for(int i = 0; i < 26; i ++){
31         //cout << i <<' ' << fun(i) << endl;
32         ans = min(ans,fun(i));
33     }
34     cout << ans << endl;
35     return 0;
36 }

 

posted @ 2017-06-18 22:53  starry_sky  阅读(188)  评论(0编辑  收藏  举报