Codeforces 510C - Fox And Names (拓扑排序)

题意

给出一串名字,代表着字典序从小到大,要求输出符合上述字符串排序的字典序。

思路

看了飞火扑蛾的题解。

学习了拓扑排序。

如果一个字符串是另一个字符串的前缀并且另一个字符串排在这个字符串之前,这样肯定是不行的。可以直接输出Impossible。

代码

 
 
  1. #include <stack>
  2. #include <cstdio>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <unordered_set>
  11. #include <cstring>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <string>
  15. #include <map>
  16. #include <cmath>
  17. using namespace std;
  18. #define LL long long
  19. #define ULL unsigned long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define X first
  26. #define Y second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. #define BitCountll(x) __builtin_popcountll(x)
  35. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  36. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  37. const double PI = acos(-1.0);
  38. const int INF = 0x3f3f3f3f;
  39. const double eps = 1e-8;
  40. const int MAXN = 30 + 10;
  41. const int MOD = 29;
  42. const int dir[][2] = { { 1, 0 }, { 0, 1 } };
  43. int cases = 0;
  44. typedef pair<int, int> pii;
  45. int mp[MAXN][MAXN], in[MAXN];
  46. vector<char> ans;
  47. const int sigma_size = 26;
  48. void BFS()
  49. {
  50. queue<int> Q;
  51. for (int i = 0; i < sigma_size; i++)
  52. if (in[i] == 0)
  53. {
  54. Q.push(i);
  55. ans.PB(i);
  56. }
  57. while (!Q.empty())
  58. {
  59. int cur = Q.front(); Q.pop();
  60. for (int i = 0; i < sigma_size; i++)
  61. {
  62. if (mp[cur][i])
  63. {
  64. in[i]--;
  65. if (in[i] == 0)
  66. {
  67. Q.push(i);
  68. ans.PB(i);
  69. }
  70. }
  71. }
  72. }
  73. if (SZ(ans) != sigma_size) cout << "Impossible" << endl;
  74. else
  75. for (auto &i: ans) cout << (char)(i+'a');
  76. }
  77. string str[110];
  78. int idx(char c) { return c - 'a'; }
  79. int main()
  80. {
  81. //ROP;
  82. int n;
  83. cin >> n;
  84. for (int i = 0; i < n; i++) cin >> str[i];
  85. int i, j;
  86. for (i = 0; i < n-1; i++)
  87. {
  88. bool flag = false;
  89. for (j = 0; j < min(SZ(str[i]), SZ(str[i+1])); j++)
  90. {
  91. if (str[i][j] == str[i+1][j]) continue;
  92. flag = true;
  93. int a = idx(str[i][j]), b = idx(str[i+1][j]);
  94. if (!mp[a][b])
  95. {
  96. mp[a][b] = 1;
  97. in[b]++;
  98. }
  99. break;
  100. }
  101. if (!flag && SZ(str[i]) > SZ(str[i+1]))
  102. {
  103. cout <<"Impossible" << endl;
  104. return 0;
  105. }
  106. }
  107. BFS();
  108. return 0;
  109. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值