###### tags: `C++` `1.input and output` # 1. ios::sync_with_stdio(0); cin.tie(0); 避免超大數據cin TLE TLE = Time Limit Exceeded 執行超出時間限制 ios::sync_with_stdio(0) 禁用C和C++標準流同步 加速cin和cout cin.tie(0); 禁止綁定cin和cout 加速cin和cout 例如 字串解碼 有這行 快了4ms 大數據會加快更多 ```c++= #include<bits/stdc++.h> using namespace std; //2022-6 P2字串解碼 參考 https://0rz.tw/PZ9ty // AC (6ms, 348KB) int main() { ios::sync_with_stdio(0); //+1行變成 AC (2ms, 360KB) cin.tie(0); //+2行變成 AC (2ms, 376KB) cout.tie(0); //+3行變成 AC (2ms, 388KB) int m,n; cin >> m >> n; //m行0101 每行n個 vector<string> v(m);//裝0101 for(int i=0; i<m; i++) cin >> v[i]; //輸入m行 01010 string cipher; //裝ADJFIW 密文 cin >> cipher; deque<char> c(cipher.begin(),cipher.end() ); //裝密文 慢慢變成 明文 for (int i=m-1; i>=0; i--)//從最後一行0101開始 { int odd = 0; //1出現次數是odd就頭尾互換 deque<char> p; //裝明文 每次重製 位置很重要 //放錯會error: 'p' was not declared in this scope| for (int j=n-1; j>=0; j--)//從0101尾巴開始 { if (v[i][j]=='1') //計算1出現次數 odd++; if (v[i][j]=='0') //0把被切掉的頭放回頭 p.push_front(c[j]); else //1把被切掉的尾放回尾 p.push_back(c[j]); } if (odd%2) //1出現次數=奇數 for (int j=0; j<n/2; j++)//頭尾互換 swap(p[j], p[n-n/2+j] ); c = p;//確定轉換完成明文再丟進plain }//for外面 不認識p 用plain才能印 for(char i : c) cout << i; } ``` 參考 1.https://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html 2.https://www.796t.com/content/1525164019.html 3.https://www.796t.com/post/MmcwYw==.html
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up