- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 60 lines of C++ from the credited upstream file abc302_c.cpp.
- The implementation visibly relies on sequence storage, ordered lookup.
- 6 loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#include <iostream>2#include <map>3#include <vector>4 5using namespace std;6using ui = unsigned int;7 8bool aeq(string s, string t) {9 ui diff = 0;10 for (ui i = 0; i < s.size(); i++) {11 diff += ui(s[i] != t[i]);12 if (diff > 1) return false;13 }14 15 return true;16}17 18bool ok(vector<string>& s, map<string, map<string, bool>> g, ui pos, string cur,19 map<string, bool> chosen) {20 if (pos == s.size()) return true;21 22 chosen[cur] = true;23 bool _ok = false;24 for (auto t : s) {25 if (chosen[t] || !g[cur][t]) continue;26 27 _ok = _ok || ok(s, g, pos + 1, t, chosen);28 }29 30 return _ok;31}32 33int main() {34 ui n, m;35 cin >> n >> m;36 37 vector<string> s(n);38 for (auto& ss : s) cin >> ss;39 40 map<string, map<string, bool>> g;41 for (ui i = 0; i < n; i++) {42 for (ui j = i + 1; j < n; j++) {43 bool v = aeq(s[i], s[j]);44 g[s[i]][s[j]] = v;45 g[s[j]][s[i]] = v;46 }47 }48 49 map<string, bool> chosen;50 for (auto t1 : s) {51 if (ok(s, g, 1, t1, chosen)) {52 cout << "Yes" << endl;53 return 0;54 }55 }56 57 cout << "No" << endl;58 59 return 0;60}