hihocoder1712 字符串排序(思维)

https://hihocoder.com/problemset/problem/1712

感觉解法呼之欲出,却出不来。。

一个很好的思路是,根据新的顺序表,把给定的n组字符串换成旧表对应的字符,然后用strcmp

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<map>
 8 #define lson l, m, rt<<1
 9 #define rson m+1, r, rt<<1|1
10 #define IO ios::sync_with_stdio(false);cin.tie(0);
11 #define INF 0x3f3f3f3f
12 typedef long long ll;
13 using namespace std;
14 int n;
15 char str[30];
16 map<char, int> mp;
17 typedef struct{
18     char s1[110];
19     char s2[110];
20 }Node;
21 Node node[1010];
22 bool cmp(const Node a, const Node b)
23 {
24     return strcmp(a.s2, b.s2)<0;
25 }
26 int main()
27 {
28     cin >> n;
29     cin >> str;
30     for(int i = 0; i < n; i++){
31         cin >> node[i].s1;
32     } 
33     for(int i = 0; i < 26; i++){
34         mp[str[i]] = i;
35     }
36     for(int i = 0; i < n; i++){
37         for(int j = 0; j < strlen(node[i].s1); j++){
38             node[i].s2[j] = mp[node[i].s1[j]]+'a';
39         }
40     }
41     sort(node, node+n, cmp);
42     for(int i = 0; i < n; i++){
43         cout << node[i].s1 << endl;
44     }
45     return 0;
46 } 

 

转载于:https://www.cnblogs.com/Surprisezang/p/8972413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值