fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <list>
  5.  
  6. #define uint unsigned int
  7.  
  8. using namespace std;
  9.  
  10. class CACHELINE
  11. {
  12. public:
  13. uint content;
  14. uint time;
  15.  
  16. CACHELINE()
  17. {
  18. content = 0;
  19. time = 0;
  20. }
  21. };
  22.  
  23. int main()
  24. {
  25. int N;
  26. int round = 1;
  27.  
  28. while(true)
  29. {
  30. cin >> N;
  31. if(N == 0)
  32. {
  33. break;
  34. }
  35.  
  36. list<CACHELINE*> caches;
  37.  
  38. printf("Simulation %d\n", round++);
  39.  
  40. string cmds;
  41. char *cmd;
  42. cin >> cmds;
  43. cmd = (char*)cmds.c_str();
  44.  
  45. //printf("%s\n", cmd);
  46.  
  47. int length = strlen(cmd);
  48.  
  49. uint time = 0;
  50. for(int i = 0; i < length; i++)
  51. {
  52. // if cmd[i] == '!' simulate!
  53. if(cmd[i] == '!')
  54. {
  55. for(auto iter = caches.begin(); iter != caches.end(); ++iter)
  56. {
  57. CACHELINE* cache = *iter;
  58. printf("%c", cache->content);
  59. }
  60. printf("\n");
  61.  
  62. // processing
  63. time++;
  64. continue;
  65. }
  66.  
  67. // Find the duplicated slot
  68. for(auto iter = caches.begin(); iter != caches.end(); ++iter)
  69. {
  70. CACHELINE* cache = *iter;
  71. if(cache->content == cmd[i])
  72. {
  73. caches.erase(iter);
  74. break;
  75. }
  76. }
  77.  
  78. // Push cmd until full
  79. if(caches.size() < N)
  80. {
  81. CACHELINE* cache = new CACHELINE;
  82. cache->content = cmd[i];
  83. cache->time = time;
  84.  
  85. caches.push_back(cache);
  86. }
  87. // Else evict the oldest one
  88. else
  89. {
  90. uint mtime = 0xFFFF;
  91. auto idx = caches.begin();
  92. for(auto iter = caches.begin(); iter != caches.end(); ++iter)
  93. {
  94. CACHELINE* cache = *iter;
  95.  
  96. if(mtime > cache->time)
  97. {
  98. mtime = cache->time;
  99. idx = iter;
  100. }
  101. }
  102. caches.erase(idx);
  103.  
  104. CACHELINE* cache = new CACHELINE;
  105. cache->content = cmd[i];
  106. cache->time = time;
  107.  
  108. caches.push_back(cache);
  109. }
  110. }
  111.  
  112. // Erase all elements in list
  113. for(auto iter = caches.begin(); iter != caches.end();)
  114. {
  115. CACHELINE* cache = *iter;
  116. delete cache;
  117. iter = caches.erase(iter);
  118. }
  119. }
  120. }
Success #stdin #stdout 0s 16064KB
stdin
5 ABC!CBA!!!!
0
stdout
Simulation 1
ABC
CBA
CBA
CBA
CBA