fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <unordered_map>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. struct Transition {
  11. int write_val;
  12. int move_dir; // -1: Left, +1: Right
  13. string next_state;
  14. };
  15.  
  16. struct StateTransitions {
  17. Transition on_zero;
  18. Transition on_one;
  19. };
  20.  
  21. int main() {
  22. ios_base::sync_with_stdio(false);
  23. cin.tie(NULL);
  24.  
  25. // 1. transitions.tsv dosyasını oku ve durum haritasını oluştur
  26. ifstream tsv("transitions.tsv");
  27. unordered_map<string, StateTransitions> machine;
  28. vector<string> state_order;
  29.  
  30. int w0, w1;
  31. char m0, m1;
  32. string n0, n1;
  33.  
  34. // Format: 0 L HI 1 R NT
  35. while (tsv >> w0 >> m0 >> n0 >> w1 >> m1 >> n1) {
  36. // Durum isimleri dosyadaki satır sırasıyla belirlenir
  37. // veya başlangıç durumu ilk satırdır.
  38. StateTransitions st;
  39. st.on_zero = {w0, (m0 == 'L' ? -1 : 1), n0};
  40. st.on_one = {w1, (m1 == 'L' ? -1 : 1), n1};
  41.  
  42. // Otomatın durum anahtarını oluştur
  43. string current_name = "S_" + to_string(state_order.size());
  44. state_order.push_back(current_name);
  45. machine[current_name] = st;
  46. }
  47.  
  48. // 2. Test senaryolarını işle
  49. int num_testcases;
  50. if (!(cin >> num_testcases)) return 0;
  51.  
  52. string line;
  53. getline(cin, line); // İlk satır sonu karakterini tüket
  54.  
  55. while (num_testcases--) {
  56. if (!getline(cin, line)) break;
  57. if (line.empty()) {
  58. num_testcases++;
  59. continue;
  60. }
  61.  
  62. stringstream ss(line);
  63. vector<int> nums;
  64. int val;
  65. while (ss >> val) {
  66. nums.push_back(val);
  67. }
  68.  
  69. // Teybi oluştur: C_1 tane '1', '0', C_2 tane '1', ...
  70. unordered_map<long long, int> tape;
  71. long long tape_idx = 0;
  72. for (size_t i = 0; i < nums.size(); ++i) {
  73. for (int k = 0; k < nums[i]; ++k) {
  74. tape[tape_idx++] = 1;
  75. }
  76. if (i + 1 < nums.size()) {
  77. tape[tape_idx++] = 0; // Sayılar arası ayırıcı
  78. }
  79. }
  80.  
  81. // Turing makinesini çalıştır
  82. long long head = 0;
  83. string current_state = state_order[0]; // Başlangıç durumu
  84.  
  85. while (machine.count(current_state)) {
  86. int current_symbol = tape[head];
  87. const Transition& trans = (current_symbol == 0)
  88. ? machine[current_state].on_zero
  89. : machine[current_state].on_one;
  90.  
  91. tape[head] = trans.write_val;
  92. head += trans.move_dir;
  93. current_state = trans.next_state;
  94. }
  95.  
  96. // Çıktı: Teypte kalan toplam 1 sayısı
  97. long long count_ones = 0;
  98. for (const auto& cell : tape) {
  99. if (cell.second == 1) {
  100. count_ones++;
  101. }
  102. }
  103.  
  104. cout << count_ones << "\n";
  105. }
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Standard output is empty