<style> html, body, .ui-content { background-color: #333; color: #ddd; } body > .ui-infobar { display: none; } .ui-view-area > .ui-infobar { display: block; } .markdown-body h1{ color: #9CCEF2; } .markdown-body h2, .markdown-body h3{ color: #B1D6CA; } .markdown-body h4, .markdown-body h5, .markdown-body h6 { color: #ddd; } .markdown-body h1, .markdown-body h2 { border-bottom-color: #ffffff69; } .markdown-body h1 .octicon-link, .markdown-body h2 .octicon-link, .markdown-body h3 .octicon-link, .markdown-body h4 .octicon-link, .markdown-body h5 .octicon-link, .markdown-body h6 .octicon-link { color: #fff; } .markdown-body img { background-color: transparent; } .ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a { color: white; border-left: 2px solid white; } .expand-toggle:hover, .expand-toggle:focus, .back-to-top:hover, .back-to-top:focus, .go-to-bottom:hover, .go-to-bottom:focus { color: white; } .ui-toc-dropdown { background-color: #333; } .ui-toc-label.btn { background-color: #191919; color: white; } .ui-toc-dropdown .nav>li>a:focus, .ui-toc-dropdown .nav>li>a:hover { color: white; border-left: 1px solid white; } .markdown-body blockquote { color: #bcbcbc; } .markdown-body table tr { background-color: #5f5f5f; } .markdown-body table tr:nth-child(2n) { background-color: #4f4f4f; } .markdown-body code, .markdown-body tt { color: #eee; background-color: rgba(230, 230, 230, 0.36); } a, .open-files-container li.selected a { color: #5EB7E0; } </style> ###### tags: `tgirc早修book` # Ch.06 題目練習 <font color="FEA0A0">**題目**</font> 1. [Zerojudge a442: Necklace Problem (NP)](https://zerojudge.tw/ShowProblem?problemid=a442) 2. [Toj 92 / 天線寶寶說你好](https://toj.tfcis.org/oj/pro/92/) 3. [Zerojudge d535: 2. 密碼驗證與擷取](https://zerojudge.tw/ShowProblem?problemid=d535) 4. [Zerojudge a038: 數字翻轉](https://zerojudge.tw/ShowProblem?problemid=a038) 5. [Zerojudge e267: 11192 - Group Reverse](https://zerojudge.tw/ShowProblem?problemid=e267) 6. [Kattis Quick Estimates](https://open.kattis.com/problems/quickestimate) 7. [Toj Reversed card open!](https://toj.tfcis.org/oj/pro/115/) 8. [Kattis Line Them Up](https://open.kattis.com/problems/lineup) 9. [Toj Hello world!](https://toj.tfcis.org/oj/pro/5/) 10. [Kattis Ragged Right](https://open.kattis.com/problems/raggedright) 11. [Toj J. Julius Caesar](https://toj.tfcis.org/oj/pro/127/) 12. [Toj 跳躍](https://toj.tfcis.org/oj/pro/113/) 13. [Kattis The Key to Cryptography](https://open.kattis.com/problems/keytocrypto) 14. [UVA Q11577 Letter Frequency](http://domen111.github.io/UVa-Easy-Viewer/?11577) 15. [Kattis Trik](https://open.kattis.com/problems/trik) 16. [Toj 4. Hello, TFcis!](https://toj.tfcis.org/oj/pro/294/) 17. [Zerojudge a011: 00494 - Kindergarten Counting Game](https://zerojudge.tw/ShowProblem?problemid=a011) :::spoiler <font color="FEA0A0">**題解**</font> > 1. [Zerojudge a442: Necklace Problem (NP)](https://zerojudge.tw/ShowProblem?problemid=a442) ```cpp= #include <bits/stdc++.h> using namespace std; int main(){ string str1, str2; while(cin >> str1 >> str2){ int count = 0; str2 += str2; if( str2.find(str1, 0) != string::npos ) count++; reverse( str1.begin(), str1.end() ); if( str2.find(str1, 0) != string::npos ) count++; if( count != 0) cout << "same\n"; else cout << "difference\n"; } return 0; } ``` 3. [Zerojudge d535: 2. 密碼驗證與擷取](https://zerojudge.tw/ShowProblem?problemid=d535) 函數解: ```cpp= #include <iostream> #include <algorithm> #include <string> using namespace std; int main(){ string s; cin >> s; string ss = s; reverse(s.begin(), s.end()); //翻轉字串,以確認是否為迴文 if(ss == s){ string ans = ""; int a, b; bool add = false; for(int i=1; i<ss.size(); i++){ a = ss[i-1] - '0'; b = ss[i] - '0'; if(b > a*2){ cout << "INCORRECT\n"; return 0; } if(a%2 == 0){ ans += ss[i - 1]; add = true; } } if(b%2 == 0){ ans += char(b + '0'); } else if(!add) cout << "0"; cout << ans << '\n'; } else cout << "INCORRECT\n"; return 0; } ``` 非函數解 ```cpp= #include <iostream> #include <algorithm> #include <string> using namespace std; int main(){ string s; cin >> s; int len = s.size(); for(int i=0; i<len/2; i++){ if(s[i] != s[len-i-1]){ cout << "INCORRECT\n"; return 0; } } string ans=""; for(int i=0; i<len-1; i++){ if( s[i+1] - s[i] > s[i] - '0' ){ cout << "INCORRECT\n"; return 0; } if( int( s[i] - '0' )%2 == 0 ) ans += s[i]; } if( int( s[len-1] - '0')%2 == 0) ans += s[len-1]; if(ans.size() == 0) cout << '0'; cout << ans << '\n'; return 0; } ``` 4. [Zerojudge a038: 數字翻轉](https://zerojudge.tw/ShowProblem?problemid=a038) ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ string str; cin >> str; reverse( str.begin(), str.end() ); cout << stoi(str); return 0; } ``` 5. [Zerojudge e267: 11192 - Group Reverse](https://zerojudge.tw/ShowProblem?problemid=e267) ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ string str; int G, n; while( cin >> G && G!=0){ cin >> str; n = str.size() / G; for(int i=0; i<G; i++){ reverse( str.begin() + i*n, str.begin() + (i+1)*n ); } cout << str << '\n'; } return 0; } ``` :::