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;
    }
  • 相关阅读:
    块元素&行内元素
    semantic ui要装什么才能使用
    float属性
    CSS 选择器
    px,em和rem
    CSS各类布局
    一个 / 引起想骂他事件
    使用fastjson 获取json字符串中的数组,再转化为java集合对象
    计算面试题
    Dubbo(二) 一次惨痛的流血事故
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5419038.html
Copyright © 2011-2022 走看看