fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. const int N=25;
  5. int n;
  6. vector<pair<char,char>>v;
  7. char pos[N];
  8. int sum='A'+'B'+'C';
  9. void solve(int n,char from,char to,char g){
  10. if(n==0)return ;
  11. if(from==to){
  12. solve(n-1,pos[n-1],to,sum-pos[n-1]-to);
  13. return ;
  14. }
  15. solve(n-1,pos[n-1],g,sum-pos[n-1]-g);
  16. v.push_back({from,to});
  17. for(int i=1;i<=n-1;i++)pos[i]=g;
  18. solve(n-1,g,to,from);
  19. }
  20. main()
  21. {
  22. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  23. cin>>n;
  24. for(int i=1;i<=n;i++){
  25. cin>>pos[i];
  26. }
  27. while(n>0&&pos[n]=='C')n--;
  28. if(n==0)return cout<<0,0;
  29. solve(n,pos[n],'C',sum-'C'-pos[n]);
  30. cout<<v.size()<<'\n';
  31. for(auto x:v)cout<<x.first<<x.second<<'\n';
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Standard output is empty