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

小红的字符串构造

https://www.nowcoder.com/practice/3e4b4dabc2e444e384c3ae62ac7dd84e

需要先填一遍字母,保证集合一样,使用map来匹配比较简单

#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <numeric>
#include <ctime>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>

using namespace std;
using ll = long long;
const ll N = 1e6 + 5, mod = 1e9 + 7, inf = 0x3f3f3f3f;

int n;

void solve() {
	string a;
	cin >> a;

	string temp = a;

	sort(temp.begin(), temp.end());

	if (temp[0] == temp[temp.size() - 1]) {
		cout << -1 << '\n';
		return ;
	}

	map<char, int>mp;

	for (auto i : a) {
		mp[i] = 2;
	}
	int num = mp.size();

	for (auto i : a) {
		bool flag = false;

		for (auto &j : mp) {

			if (num) {
				if (j.first != i && j.second == 2) {
					cout << j.first;
					j.second = 1;
					flag = true;
					num--;
					break;
				}
			} else {
				if (j.first != i) {
					cout << j.first;
					flag = true;
					break;
				}
			}


		}

		if (!flag) {
			for (auto j : mp) {
				if (j.first != i) {
					cout << j.first;
					break;
				}
			}
		}
	}
	cout << '\n';
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	int t = 1;
	//cin>>t;

	while (t--) {

		solve();

	}

	return 0;
}

全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
心中的变压器:简历需要突出重点
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务