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 } 

猜你喜欢

转载自www.cnblogs.com/Surprisezang/p/8972413.html