Advertisement
Dmaxiya

投入严厉地本地 参考代码

Mar 31st, 2025
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 100 + 100;
  6. int T, k;
  7. string s, t;
  8. string ss[maxn];
  9. map<string, char> ans;
  10.  
  11. bool judgeAns() {
  12.     string tt = s.substr(0, k);
  13.     for (int i = k; i < s.length(); ++i) {
  14.         map<string, char>::iterator it = ans.find(ss[i]);
  15.         if (it == ans.end()) {
  16.             tt.push_back(s[i]);
  17.             continue;
  18.         }
  19.         if (it->second == 0) {
  20.             continue;
  21.         }
  22.         tt.push_back(it->second);
  23.     }
  24.     return tt == t;
  25. }
  26.  
  27. void outputAns() {
  28.     cout << ans.size() << endl;
  29.     for (map<string, char>::iterator it = ans.begin(); it != ans.end(); ++it) {
  30.         cout << "(" << it->first << ",";
  31.         if (it->second != 0) {
  32.             cout << it->second;
  33.         }
  34.         cout << ")" << endl;
  35.     }
  36. }
  37.  
  38. bool dfs(int depthS, int depthT) {
  39.     if (depthS == s.length()) {
  40.         if (depthT == t.length() && judgeAns()) {
  41.             outputAns();
  42.             return true;
  43.         }
  44.         return false;
  45.     }
  46.  
  47.     map<string, char>::iterator it = ans.find(ss[depthS]);
  48.     if (it == ans.end()) {
  49.         ans[ss[depthS]] = 0;
  50.         if (dfs(depthS + 1, depthT)) {
  51.             return true;
  52.         }
  53.         ans[ss[depthS]] = t[depthT];
  54.         if (dfs(depthS + 1, depthT + 1)) {
  55.             return true;
  56.         }
  57.         ans.erase(ss[depthS]);
  58.         return false;
  59.     }
  60.  
  61.     if (it->second == 0) {
  62.         return dfs(depthS + 1, depthT);
  63.     }
  64.     if (it->second != t[depthT]) {
  65.         return false;
  66.     }
  67.     return dfs(depthS + 1, depthT + 1);
  68. }
  69.  
  70. int main() {
  71. #ifdef ExRoc
  72.     freopen("test.txt", "r", stdin);
  73. #endif // ExRoc
  74.     ios::sync_with_stdio(false);
  75.  
  76.     cin >> T;
  77.     while (T--) {
  78.         ans.clear();
  79.         cin >> s >> t >> k;
  80.         s = " " + s;
  81.         t = " " + t;
  82.         for (int i = k; i < s.length(); ++i) {
  83.             ss[i] = s.substr(i - k + 1, k);
  84.         }
  85.         dfs(k, k);
  86.     }
  87.  
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement