zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 12 C. Simple Strings 贪心

    C. Simple Strings

    题目连接:

    http://www.codeforces.com/contest/665/problem/C

    Description

    zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add 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.

    Sample Input

    aab

    Sample Output

    bab

    Hint

    题意

    现在给你一个串,让你使得相邻的字符都不一样,要求修改的字符最少

    问你最后的字符串长什么样

    题解:

    贪心,如果这个位置一样,那就变化就好了~

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    string s;
    int main()
    {
        cin>>s;
        s+='a';
        for(int i=1;i<s.size()-1;i++)
        {
            if(s[i]==s[i-1])
            {
                for(int j=0;j<26;j++)
                {
                    if(j+'a'==s[i-1]||(j+'a')==s[i+1])
                        continue;
                    s[i]=char(j+'a');
                    break;
                }
            }
        }
        for(int i=0;i<s.size()-1;i++)cout<<s[i];
        cout<<endl;
    }
  • 相关阅读:
    js 控制页面跳转的5种方法
    弹性盒
    js中变量声明有var和没有var的区别
    解决Cannot read property 'style' of null中样式问题
    JS中用for循环解决重复定义的问题
    博客第一天
    jQuery 无缝轮播
    随鼠标移动
    倒计时
    轮播图
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5419038.html
Copyright © 2011-2022 走看看