题目链接:点击打开链接
题解:当时写的时候卡题意卡了十年。一直没明白这个”膜法“的具体效果。明白”膜法“的效果之后。先定义一个标记,标记的用途是我们现在在找a-z中那个字符。我们只需要字符串从头扫一边,边扫边判断这个位置是否能变成标记。能的话标记+1,然后继续向后找。若在遍历结束之前标记等于z了。那么就是可以,否者就是不行。
具体实现过程看代码注释:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
char s[100005];
cin >> s;
int len = strlen(s);
int flag = 0;
if(len <= 25){ // 若长度小于25那么一定没有a-z的子序列
cout << "-1" << endl;
return 0;
}
int ans = 97; // 标记 用来表示现在在找那个字符。定义为97的原因是a的阿斯克码是97
int a[26]; // 记录那个位置变成了那个字符,因为我们要输出 变化后的序列
memset(a,0,sizeof(a));
int cur = 0 ;
for(int i = 0 ; i < len ; i ++){ // 运用“膜法”中
if(cur == 26)
break;
if(s[i] <= ans){
ans++;
a[cur++] = i;
}
}
if(a[25]){
int num = 0;
for(int i = 0 ; i < len ; i ++){
if(i == a[num]){
printf("%c",num+97);
num++;
}
else
printf("%c",s[i]);
}
}
else
cout << "-1" << endl;
}