cf Educational Codeforces Round 39 C. String Transformation

原题:
C. String Transformation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of |s| small english letters.

In one move you can replace any character of this string to the next character in alphabetical order (a will be replaced with b, s will be replaced with t, etc.). You cannot replace letter z with any other letter.

Your target is to make some number of moves (not necessary minimal) to get string abcdefghijklmnopqrstuvwxyz (english alphabet) as a subsequence. Subsequence of the string is the string that is obtained by deleting characters at some positions. You need to print the string that will be obtained from the given string and will be contain english alphabet as a subsequence or say that it is impossible.

Input
The only one line of the input consisting of the string s consisting of |s| (1 ≤ |s| ≤ 105) small english letters.

Output
If you can get a string that can be obtained from the given string and will contain english alphabet as a subsequence, print it. Otherwise print «-1» (without quotes).

Examples
input
aacceeggiikkmmooqqssuuwwyy
output
abcdefghijklmnopqrstuvwxyz
input
thereisnoanswer
output
-1

中文:
给你一个字符串,然后给你一个变换规则,每个字符只能变成比它小的字符,现在问你能不能把这个字符串变成一个包含一个子串,这个子串是a到z的26个字母。

代码:

#include
using namespace std;
char s[100005];

//string s;

int main ()
{
    ios::sync_with_stdio(false);
    while(cin>>s)
    {
        int len=strlen(s);
        if(len<26)
        {
            cout<<-1<continue;
        }
        int ind=0;
        while(s[ind]!='a')
            ind++;
        if(len-ind<26)
        {
            cout<<-1<continue;
        }
        char pre='a';
        int flag=0;
        for(int i=ind+1;iif(s[i]<=pre)
            {
                s[i]=pre+1;
                pre=s[i];
            }
            else
            {
                if(s[i]==pre+1)
                    pre=s[i];
            }
            if(pre=='z')
            {
                flag=1;
                break;
            }
        }
        if(flag)
            cout<else
            cout<<-1<return 0;
}

解答:

首先判断长度是否小于26,如果长度不够26肯定不行。

然后再判断字符a的位置,如果字符串a的位置后面不够26个,还是不行。

找到字符a以后,接下来找能变成字符b的字符,依次类推,如果最后能找到一个字符,这个字符能变成字符z,那么大功告成,否则不行。

你可能感兴趣的:(贪心\模拟\STL\暴力)