题目
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.
输入
The only one line of the input consisting of the string s consisting of |s| (1 ≤ |s| ≤ 105) small english letters.
输出
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).
样例
样例1输入
aacceeggiikkmmooqqssuuwwyy
输出
abcdefghijklmnopqrstuvwxyz
样例2输入
thereisnoanswer
输出
-1
题意
给出一个字符串,每次操作,你可以将其中的一个字符变成26个英文字母中处于它后面的任意一个字母。
如:a可以变成b~z中任意一个字符,字符z是无法变动的。
问你是否能够通过一系列操作后使得字符串包含a~z?
可以就输出改变之后的字符串,不可以就输出-1。
思路
首先如果字符串的长度小于26就直接输出-1;
长度大于等于26的字符串,我们进行如下操作:
我们设置一个字符ch代表当前处于26个字母中的位置(默认值为a)。
然后我们从头遍历,如果当前的字符<=ch,则把当前字符改为ch,然后让ch向后移。
可以修改成功的条件就是移动了26次,所以再用个变量记录后移次数。
具体看代码,伊丽莎白!
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string ss;
cin>>ss;
if(ss.size()<26)
cout<<"-1"<<endl;
else
{
int ans=0,flag=0;
char ch='a';
for(int i=0;i<ss.size();i++)
{
if(ss[i]<=ch)
{
ss[i]=ch;
ch++;
ans++;
}
if(ans==26)
{
flag=1;
break;
}
}
if(flag)
cout<<ss<<endl;
else
cout<<"-1"<<endl;
}
}
虽然是C题但其实只要读懂题意,也不一定是一道难题,你上你也行👍。
下次再见~