思路:
将字符串按照新的顺序映射之后再排序。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 string s, t; 8 cin >> n >> t; 9 map<char, int> mp; 10 for (int i = 0; i < t.length(); i++) mp[t[i]] = i; 11 vector<string> str; 12 for (int i = 0; i < n; i++) 13 { 14 cin >> s; 15 str.push_back(s); 16 } 17 vector<pair<string, int>> vs; 18 for (int i = 0; i < n; i++) 19 { 20 int l = str[i].length(); 21 string res = ""; 22 for (int j = 0; j < l; j++) 23 { 24 res += (char)(mp[str[i][j]] + 'a'); 25 } 26 vs.push_back(make_pair(res, i)); 27 } 28 sort(vs.begin(), vs.end()); 29 for (int i = 0; i < vs.size(); i++) cout << str[vs[i].second] << endl; 30 return 0; 31 }