CF1277D Let's Play the Words?
Posted wangyiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1277D Let's Play the Words?相关的知识,希望对你有一定的参考价值。
思路:
字符串其实只有0...0, 0...1, 1...0, 1...1四种。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 set<string> st[4]; 4 bool work(set<string>& a, set<string>& b, map<string, int>& mp, vector<int>& res) 5 { 6 int d = a.size() - b.size(); 7 for (auto it: a) 8 { 9 if (res.size() == d / 2) break; 10 string tmp = it; 11 reverse(tmp.begin(), tmp.end()); 12 if (b.count(tmp)) continue; 13 res.push_back(mp[it]); 14 } 15 return res.size() >= d / 2; 16 } 17 int main() 18 { 19 int t; cin >> t; 20 while (t--) 21 { 22 for (int i = 0; i < 4; i++) st[i].clear(); 23 int n; cin >> n; 24 map<string, int> mp; 25 for (int i = 0; i < n; i++) 26 { 27 string s; cin >> s; 28 mp[s] = i + 1; 29 int a = *s.begin() - ‘0‘, b = *(s.end() - 1) - ‘0‘; 30 int p = a * 2 + b; 31 st[p].insert(s); 32 } 33 if (!st[0].empty() && !st[3].empty() && st[1].empty() && st[2].empty()) 34 { 35 cout << -1 << endl; continue; 36 } 37 vector<int> res; 38 bool ok = false; 39 if (st[1].size() > st[2].size()) ok = work(st[1], st[2], mp, res); 40 else ok = work(st[2], st[1], mp, res); 41 if (ok) 42 { 43 cout << res.size() << endl; 44 for (auto it: res) cout << it << " "; 45 cout << endl; 46 } 47 else cout << -1 << endl; 48 } 49 return 0; 50 }
以上是关于CF1277D Let's Play the Words?的主要内容,如果未能解决你的问题,请参考以下文章
[Codeforces Round #146 (Div. 1) B]Let's Play Osu!(期望Dp)