[关闭]
@wsndy-xx 2018-05-05T03:24:20.000000Z 字数 839 阅读 832

hihocoder 字符串排序解题报告

                        wsndy
                        18.05.04

题解

题意

定义新的字符字典序大小
然后给出 个字符串
按照新的字典序排序后从小到大输出


思路

其实不需要重新定义(应该也没法重新定义)
只需将新的字典序映射到原先的字典序中
就是将每个字符串 映射出一个
例如

  1. 新的字典序 bdceafghijklmnopqrstuvwxyz
  2. 字符 S = abcde

那么

  1. eacbd

那么只需按照 的大小排序后
输出 即可


Code

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. const int N = 1010;
  5. int Map[30], n;
  6. struct Node {std:: string ture_string, map_string;}E[N];
  7. std:: string A;
  8. inline bool cmp(Node a, Node b) {return a.map_string < b.map_string;}
  9. int main() {
  10. std:: cin >> n;
  11. std:: cin >> A;
  12. for(int i = 1; i <= n; i ++) std:: cin >> E[i].ture_string;
  13. int Len = A.length();
  14. for(int i = 0; i < Len; i ++) Map[A[i] - 'a' + 1] = i + 1;
  15. for(int i = 1; i <= n; i ++) {
  16. int L = E[i].ture_string.length();
  17. for(int j = 0; j < L; j ++)
  18. E[i].map_string += Map[E[i].ture_string[j] - 'a' + 1] + 'a' - 1;
  19. }
  20. std:: cout << E[1].map_string;
  21. std:: sort(E + 1, E + n + 1, cmp);
  22. for(int i = 1; i <= n; i ++) std:: cout << E[i].ture_string << "\n";
  23. return 0;
  24. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注