<style> html, body, .ui-content { background: #222222; color: #00BFFF; } ::-webkit-scrollbar { width: 10px; } ::-webkit-scrollbar-track { background: transparent; } ::-webkit-scrollbar-thumb { background: linear-gradient(180deg, #2BE8CF60 0%, #2B83E860 100%); border-radius: 3px; } ::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, #2BE8CF95 0%, #2B83E895 100%); } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } </style> ###### tags: `Codeforces` # H. Beppa and SwerChat ## 題目 ###### Link : https://codeforces.com/contest/1776/problem/H ## 程式碼 ```cpp= #include <bits/stdc++.h> using namespace std; /* before: 1 4 2 5 3 after: 4 5 1 2 3 由after去看before (由右邊往左邊找) 1. 先看after最右邊 : 3 從before的最左邊找到 3 記錄好位置 2. 再看after的 2 再從記錄好的位置往左邊找到 2 記錄好位置 3. 再看after的 1 再從記錄好的位置往左邊找到 1 記錄好位置 發現before已經找到最左邊了 那after剩下的數字 必定再9.和22.間有登入 */ int main() { int Case; cin >> Case; while (Case--) { int time; cin >> time; vector<int> before(time), after(time); for (int i = 0; i < time; ++i) { cin >> before[i];//9.前的登入 } for (int i = 0; i < time; ++i) { cin >> after[i];//22.後的登入 } int flag = time - 1, ans; for (int i = time - 1; i >= 0; --i) { for (int j = flag;; --j) { if (j < 0) { flag = j; ans = i + 1; break; } if (after[i] == before[j]) { flag = j; ans = i; break; } } if (flag < 0) { break; } } cout << ans << endl; } return 0; } ```