zoukankan      html  css  js  c++  java
  • AtCoder Grand Contest 016(A

    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 }
  • 相关阅读:
    UVALive4727:jump
    UVALive
    UVA11795 Mega Man's Mission
    UVA4731:Cellular Network
    UVA11404:Palindromic Subsequence
    设计思路
    阅读计划
    上课未完成代码原因
    《人月神话》读后感
    《软件工程》第十一章总结
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7045777.html
Copyright © 2011-2022 走看看