2021/11/16 traing
今天vp了一场,发现问题还是挺大的
#include <bits/stdc++.h> using namespace std; int T,n; int a,b,c; void solve() { scanf("%d %d %d",&a,&b,&c); int mul = a + c - 2 * b; mul %= 3; mul += 3; mul %= 3; printf("%d\n",min(mul,3 - mul)); } int main() { T = 1; scanf("%d",&T); while(T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int T,n; string str; string pre; vector<int> G; void solve() { G.clear(); cin >> n; cin >> str; int cnt = 0; for(int i = 0;i < str.length();++i) { if(str[i] == '0') cnt++; } pre = ""; for(int i = 0;i < str.length();++i) { if(i <= cnt - 1) pre += "0"; else pre += "1"; } for(int i = 0;i < str.length();++i) { if(str[i] != pre[i]) G.emplace_back(i + 1); } if(G.size() == 0) { cout << "0"<<"\n"; } else { cout << "1" <<"\n"; cout << G.size() <<" "; for(auto h : G) { cout << h <<" "; } cout << "\n"; } } int main() { T = 1; scanf("%d",&T); while(T--) { solve(); } return 0; }
#include <bits/stdc++.h> #define FAST std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); using namespace std; int T,n; string str; string pre; vector<int> G; int num[10]; void solve() { G.clear(); cin >> n; cin >> str; for(int i = 0;i < str.length();++i) { if(str[i] == 'a') { G.emplace_back(i); } } int ans = 0x3f3f3f3f; for(int i = 1;i < G.size();++i) { if(G[i] - G[i - 1] <= 2) { ans = min(ans,G[i] - G[i - 1] + 1); } else if(G[i] - G[i - 1] == 3) { if(str[G[i - 1] + 1] != str[G[i - 1] + 2]) ans = min(ans,4); else { if(i != G.size() - 1 && G[i + 1] - G[i] == 3 && str[G[i + 1] - 1] != str[G[i - 1] + 1]) { ans = min(ans,7); } } } } if(ans == 0x3f3f3f3f) ans = -1; cout << ans <<"\n"; } int main() { //FAST T = 1; cin >> T; while(T--) { solve(); } return 0; }
还有一种更加简便的写法
#include <bits/stdc++.h> #define FAST std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); using namespace std; int T,n; string str; string pre; vector<int> G; int num[10]; string com[] = {"aa","aba","aca","abca","acba","abbacca","accabba"}; void solve() { cin >> n; cin >> str; for(auto f:com) { if(str.find(f) != str.npos) { cout << f.length()<<"\n"; return; } } cout << "-1" <<"\n"; } int main() { //FAST T = 1; cin >> T; while(T--) { solve(); } return 0; }