UVALive 6499 - sort me

题目是PDF文档格式的,所以直接贴题目链接吧:

题目链接

一般我们对字符串排序是根据ABC……XYZ的字母序进行排序,现在题目给出新的字母序,和待排序的字符串;        
要求根据这些新的字母序题意是给出新的排序后的字符串
我想一般人的想法肯定是类比ABC……XYZ序列,再排序吧
我的想法就是为每一个待排序的字符串关联一个字符串,在这个关联字符串中保存当前字符串对应的ABC……XYZ序
之后对每个字符串的关联字符串排序即可。。。真的是脑海中闪现的方法,也没想对不对就开始敲代码了。。。。


代码如下:

#include <map>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define esp 1e-9
#define MAXN 10010
#define ll long long
#define INF 0x7FFFFFFF
#define BUG system("pause")
#define SW(a,b) a^=b;b^=a;a^=b;
using namespace std;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string ch[MAXN];
string chs[MAXN];
int main(void){
	int n;
	string str;
	int count = 1;
	while(cin >> n, n){
		cin >> str;
		for(int i=0; i<n; ++i){
			cin >> ch[i];
			chs[i].clear();
			for(int j=0; j<ch[i].size(); ++j){
//				alpha[
				chs[i].push_back( alpha[str.find(ch[i][j])]);
//				ch[i][j] = alpha[p-str];
			}
		}
		for(int i=0; i<n; ++i){
			for(int j=i+1; j<n; ++j){
				if(chs[i] > chs[j]){
					string temp = ch[i];
					ch[i] = ch[j];
					ch[j] = temp;
					temp = chs[i];
					chs[i] = chs[j];
					chs[j] = temp;
				}
			}
		}
		cout << "year " << count++ << endl;
		for(int i=0; i<n; ++i){
			cout << ch[i] << endl;
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值