思路:
字符串其实只有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 }