NYIST 542 - 试制品 (STL + 小暴力)

来源:互联网 发布:淘宝极速退款条件 编辑:程序博客网 时间:2024/06/07 00:35

思路

用map先把各种反应物生成物编号,然后不断扫描反应物,如果有一个式子的反应物充足,记下生成物,继续扫描。

直到扫描完全部的反应式没有生成物生成。

读取反应物生成物这块有点小麻烦。。

我用了两个数组,分别存反应物和生成物。然后各种STL。。。

详情见代码

代码

  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. const double PI = acos(-1.0);
  31. const int INF = 0x3f3f3f3f;
  32. using namespace std;
  33. const int MAXN = 1000 + 10;
  34. const int MOD = 1e9 + 7;
  35. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  36. typedef pair<int, int> pii;
  37. typedef vector<int>::iterator VITI;
  38. typedef vector<pii>::iterator VITII;
  39. map<int, string> rmp;
  40. map<string, int> mp;
  41. vector<int> react[MAXN], result[MAXN];
  42. vector<string> out;
  43. int tot, vis[MAXN], add[MAXN], ans;
  44. void Solve()
  45. {
  46. int n;
  47. scanf("%d", &n);
  48. string tmp;
  49. for (int i = 0; i < n; i++)
  50. {
  51. cin >> tmp;
  52. if (mp.count(tmp)) vis[mp[tmp]] = 1;
  53. }
  54. while (true)
  55. {
  56. bool flag = false;
  57. VITI it;
  58. for (int i = 1; i <= tot; i++)
  59. {
  60. if (add[i]) continue; //如果已经统计过的跳过
  61. for (it = react[i].begin(); it != react[i].end(); it++)
  62. if (!vis[*it]) break;
  63. if (it == react[i].end()) //说明反应物充足,接下来统计生成物
  64. {
  65. for (VITI iti = result[i].begin(); iti != result[i].end(); iti++)
  66. if (!vis[*iti])
  67. {
  68. vis[*iti] = 1;
  69. ans++;
  70. out.PB(rmp[*iti]);
  71. }
  72. add[i] = 1; flag = true;
  73. }
  74. }
  75. if (!flag) break;
  76. }
  77. sort(out.begin(), out.end());
  78. cout << ans << endl;
  79. for (int i = 0; i < SZ(out); i++) cout << out[i] << endl;
  80. }
  81. void Init(int n)
  82. {
  83. MS(vis, 0); MS(add, 0);
  84. mp.clear(), rmp.clear();
  85. out.clear();
  86. for (int i = 0; i <= n; i++)
  87. {
  88. react[i].clear();
  89. result[i].clear();
  90. }
  91. }
  92. char str[110];
  93. int main()
  94. {
  95. //ROP;
  96. int n, i, j;
  97. while (~scanf("%d", &tot))
  98. {
  99. Init(tot);
  100. int number = 0;
  101. for (i = 1; i <= tot; i++)
  102. {
  103. scanf("%s", str);
  104. string tmp;
  105. int cnt = 0;
  106. for (j = 0; j < strlen(str); j++)
  107. {
  108. if (str[j] == '=')
  109. {
  110. cnt = 1;
  111. if (!mp.count(tmp))
  112. {
  113. mp[tmp] = number;
  114. rmp[number++] = tmp;
  115. }
  116. react[i].PB(mp[tmp]);
  117. tmp.clear();
  118. continue;
  119. }
  120. if (cnt == 0)
  121. {
  122. if (str[j] != '+') tmp += str[j];
  123. else
  124. {
  125. if (!mp.count(tmp))
  126. {
  127. mp[tmp] = number;
  128. rmp[number++] = tmp;
  129. }
  130. react[i].PB(mp[tmp]);
  131. tmp.clear();
  132. }
  133. }
  134. else
  135. {
  136. if (str[j] != '+') tmp += str[j];
  137. else
  138. {
  139. if (!mp.count(tmp))
  140. {
  141. mp[tmp] = number;
  142. rmp[number++] = tmp;
  143. }
  144. result[i].PB(mp[tmp]);
  145. tmp.clear();
  146. }
  147. }
  148. }
  149. if (!mp.count(tmp))
  150. {
  151. mp[tmp] = number;
  152. rmp[number++] = tmp;
  153. }
  154. result[i].PB(mp[tmp]);
  155. }
  156. Solve();
  157. }
  158. return 0;
  159. }
0 0
原创粉丝点击