H. Digit string
Title link: https://ac.nowcoder.com/acm/contest/9983/H
Title description:
Niuniu discovered a way to convert a string containing only lowercase letters into a string of numbers in the following way:
take each letter in it, convert a to 1, b to 2...z to 26, and then These numbers are stitched together.
For example, abcz can be converted to 12326.
Now given a string S containing only lowercase letters, you need to find a string T containing only lowercase letters, so that the two strings are not the same but can be converted into the same string of numbers.
Enter a description:
A string of lowercase letters S with a length of no more than 10^6 per line.
Output description:
A string of lowercase letters T with a length not exceeding 2×10^6 per line.
If there is no solution, please output -1.
If the answer has a solution and the string you output contains characters other than lowercase letters or the length exceeds 2×10^6, then you will get the return result of "Answer Wrong".
Otherwise, if the answer has a solution and your answer and the input string can be converted to the same string of numbers, then your answer will be considered correct.
Example 1:
Enter
cwc and
output
cbcc,
indicating that
the digital strings converted by cwc and cbcc are both 3233
Example 2:
Input
ccc
output
-1
Problem-solving ideas:
Consider whether a character can be split, and whether two adjacent characters can be merged, otherwise there is no solution to output -1.
Convert the given letter string into a number string, ① use two digits first, ② use single digits first (character one-to-one correspondence conversion) If the letter strings constructed by the two schemes are equal, no solution will output -1. Otherwise, output any string that is not equal to the initial string.
code show as below:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
typedef long long ll;
int main(){
string s;
ll i;
cin>>s;
string x="",y="";
ll len=s.length();
for(i=0;i<len;i++){
ll t=s[i]-'a'+1;
if(t/10>0 && t%10!=0){
x += t/10+'a'-1;
x += t%10+'a'-1;
}
else x += s[i];
}
if(x != s){
cout<<x;
return 0;
}
for(i=0;i<len-1;i++){
ll x1=s[i]-'a'+1;
ll x2=s[i+1]-'a'+1;
ll cnt=x1*10+x2;
if(x2<10&&cnt<=26){
y += cnt+'a'-1; i++;
}else
y+=s[i];
}
if(i==len-1)
y+=s[i];
if(y != s)
cout<<y;
else
cout<<"-1";
return 0;
}