Codeforces 946 C.String Transformation

本文介绍了一个字符串转换的问题,目标是通过替换字符使给定字符串包含英语字母表作为子序列。提供了输入输出示例及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

C. String Transformation
 
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
Copy
aacceeggiikkmmooqqssuuwwyy
output
abcdefghijklmnopqrstuvwxyz
input
Copy
thereisnoanswer
output
-1

代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<cstdlib>
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn=1e5+10;
10 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
11 int main(){
12     string s;
13     ios;
14     cin>>s;
15     int len=s.length();
16     int tmp=0;
17     for(int i=0;i<len;i++){
18         if(s[i]-'a'<=tmp){
19             tmp++;s[i]='a'+tmp-1;
20         }
21         if(tmp==26)break;
22     }
23     if(tmp==26)cout<<s<<endl;
24     else cout<<"-1"<<endl;
25 }

 

 

溜了,滚去写D的题解。

 

 

转载于:https://www.cnblogs.com/ZERO-/p/9711411.html

You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters. Determine whether it is possible to make S identical to T by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required. Choose two lowercase English letters x,y and replace every occurrence of x in S with y. Constraints 1≤N≤2×10 5 N is an integer. Each of S and T is a string of length N, consisting of lowercase English letters. Input The input is given from Standard Input in the following format: N S T Output If it is possible to make S identical to T, print the minimum number of operations required. Otherwise, print −1. Sample Input 1 Copy 6 afbfda bkckbb Sample Output 1 Copy 4 By performing the operation four times in the following way, you can make S identical to T: Choose x= b and y= c. S becomes afcfda. Choose x= a and y= b. S becomes bfcfdb. Choose x= f and y= k. S becomes bkckdb. Choose x= d and y= b. S becomes bkckbb, which is identical to T. It cannot be done with fewer than four operations, so the minimum number of operations required is 4. Sample Input 2 Copy 4 abac abac Sample Output 2 Copy 0 S and T are already identical, so no operations are required. Sample Input 3 Copy 4 abac abrc Sample Output 3 Copy -1 No matter how you repeat the operation, it is impossible to make S identical to T. Sample Input 4 Copy 4 abac bcba Sample Output 4 Copy 4 C++,中文!!!
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值