fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lli long long int
  4. #define pb push_back
  5.  
  6. vector<lli> v;
  7. unordered_map<char, int> m;
  8. vector<string> ans;
  9. string s;
  10. lli n;
  11. string a, b;
  12. void doMan(lli n)
  13. {
  14. m.clear();
  15. for (lli i = 0; i < n; i++)
  16. m[a[i]] = i;
  17. lli cnt = 0;
  18. char ch ;
  19. while (a != b)
  20. {
  21. cnt++;
  22. for (lli i = 0; i < n; i++)
  23. if (a[i] != b[i])
  24. {
  25. ch = min(a[i], b[i]);
  26. v.pb(i);
  27. }
  28. v.pb(m[ch]);
  29. //cout<<v.size()<<" ";
  30. s += to_string(v.size());
  31. s += " ";
  32. for (lli x : v)
  33. {
  34. a[x] = ch;
  35. s += to_string(x);
  36. s += " ";
  37. }
  38. //cout<<x<<" ";
  39. v.clear();
  40. ans.pb(s);
  41. s.clear();
  42. //a = b;
  43. }
  44. cout << cnt << "\n";
  45. for (auto x : ans)
  46. cout << x << "\n";
  47. }
  48.  
  49. void solve()
  50. {
  51. cin >> n;
  52. cin >> a >> b;
  53. for (int i = 0; i < n; i++)
  54. m[b[i]]++;
  55. for (int i = 0; i < n; i++)
  56. {
  57. if (m[a[i]] > 0)
  58. m[a[i]] = 0;
  59. }
  60. lli f = 0;
  61. for (auto x : m)
  62. {
  63. if (x.second > 0)
  64. {
  65. cout << "-1" << "\n";
  66. f = 1;
  67. break;
  68. }
  69. }
  70. if (f != 1)
  71. doMan(n);
  72. }
  73. int main()
  74. {
  75. #ifndef ONLINE_JUDGE
  76. freopen("input.txt", "r", stdin);
  77. freopen("output.txt", "w", stdout);
  78. #endif
  79.  
  80. ios_base::sync_with_stdio(false);
  81. cin.tie(0);
  82. cout.tie(0);
  83.  
  84. lli test;
  85. cin >> test;
  86. while (test--)
  87. {
  88. solve();
  89. }
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0s 4176KB
stdin
3
5
abcab
aabab
3
aaa
aab
2
de
cd
stdout
2
3 1 2 4 
2 1 3 
-1
-1