题解 | #小红的字符串构造#

小红的字符串构造

https://www.nowcoder.com/practice/3e4b4dabc2e444e384c3ae62ac7dd84e?tpId=376&tqId=10823894&ru=/exam/oj&qru=/ta/15-days-help/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E9%259D%25A2%25E8%25AF%2595%26topicId%3D376

思路:

凯撒密码。收集字符串 s 里出现的字母,用 set 默认排序,得到 abcd......使字母偏移一位即可,a->b, b->c, c->d, ......, z->a

再遍历字符串 s,替换 s 的字母即可


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX=1e9+7;

int t,n,m;
void solve(){
	string s;
	cin>>s;
	set<char> se;
	for(int i=0;i<s.size();i++)
		se.insert(s[i]);
    if(se.size()<2)
    {
        cout<<-1;
        return;
    }
	map<char,char> mp;
	for(auto it=se.begin();it!=se.end();it++)
	{
		auto p=it;
		if((++p)!=se.end())mp[*it]=*(p);
		else mp[*it]=*se.begin();
	}
	string t;
	for(int i=0;i<s.size();i++)
		t+=mp[s[i]];
	cout<<t<<endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#牛客创作赏金赛#
牛客竞赛题解 文章被收录于专栏

个人向题解,用于存档

全部评论

相关推荐

评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务