Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int maxn = 100 + 100;
- int T, k;
- string s, t;
- string ss[maxn];
- map<string, char> ans;
- bool judgeAns() {
- string tt = s.substr(0, k);
- for (int i = k; i < s.length(); ++i) {
- map<string, char>::iterator it = ans.find(ss[i]);
- if (it == ans.end()) {
- tt.push_back(s[i]);
- continue;
- }
- if (it->second == 0) {
- continue;
- }
- tt.push_back(it->second);
- }
- return tt == t;
- }
- void outputAns() {
- cout << ans.size() << endl;
- for (map<string, char>::iterator it = ans.begin(); it != ans.end(); ++it) {
- cout << "(" << it->first << ",";
- if (it->second != 0) {
- cout << it->second;
- }
- cout << ")" << endl;
- }
- }
- bool dfs(int depthS, int depthT) {
- if (depthS == s.length()) {
- if (depthT == t.length() && judgeAns()) {
- outputAns();
- return true;
- }
- return false;
- }
- map<string, char>::iterator it = ans.find(ss[depthS]);
- if (it == ans.end()) {
- ans[ss[depthS]] = 0;
- if (dfs(depthS + 1, depthT)) {
- return true;
- }
- ans[ss[depthS]] = t[depthT];
- if (dfs(depthS + 1, depthT + 1)) {
- return true;
- }
- ans.erase(ss[depthS]);
- return false;
- }
- if (it->second == 0) {
- return dfs(depthS + 1, depthT);
- }
- if (it->second != t[depthT]) {
- return false;
- }
- return dfs(depthS + 1, depthT + 1);
- }
- int main() {
- #ifdef ExRoc
- freopen("test.txt", "r", stdin);
- #endif // ExRoc
- ios::sync_with_stdio(false);
- cin >> T;
- while (T--) {
- ans.clear();
- cin >> s >> t >> k;
- s = " " + s;
- t = " " + t;
- for (int i = k; i < s.length(); ++i) {
- ss[i] = s.substr(i - k + 1, k);
- }
- dfs(k, k);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement