文章

34

粉丝

0

获赞

6

访问

984

头像
字符串排序2 题解:
P1255 北京大学机试题
发布于2025年8月6日 17:41
阅读数 19

#include<bits/stdc++.h>
using namespace std;

int main(){
	string line;
	while(getline(cin, line)){
		vector<char> letters;
		
		// 提取并存储字母
		for(char &c : line){
			if(isalpha(c)) letters.push_back(c);
		}
		
		// 对字母序列进行稳定排序
		stable_sort(letters.begin(), letters.end(), [](char a, char b){
			return tolower(a) < tolower(b);
		});
		
		// 重构字符串,跳过原有的非字母进行字母插入
		int idx = 0;
		for(char &c : line){
			if(isalpha(c)) c = letters[idx ++];
		}
		
		// 输出排序后的结果
		cout << line << '\n';
	}
	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发