fork download
  1. #include<bits/stdc++.h>
  2. #define re register
  3. #define li inline
  4. using namespace std;
  5. int n,ans=99;
  6. string x,y,a[10],b[10];
  7. li bool pd(re string z)
  8. {
  9. if(y.size()!=z.size()) return 0;
  10. for(re int i=0;i<z.size();i++)
  11. if(y[i]!=z[i]) return 0;
  12. return 1;
  13. }
  14. li void dfs(re int last,re int dep,re string z)
  15. {
  16. if(dep==10) return ;
  17. if(pd(z))
  18. {
  19. ans=min(ans,dep);
  20. return ;
  21. }
  22. for(re int i=1;i<=n;i++)
  23. {
  24. if(i==last) continue;
  25. int p=z.find(a[i]);
  26. if(p==string::npos) continue;
  27. string s=z;
  28. dfs(i,dep+1,s.replace(p,a[i].size(),b[i]));
  29. }
  30. }
  31. int main()
  32. {
  33. cin>>x>>y;
  34. while(cin>>a[++n]>>b[n+1]);
  35. dfs(0,0,x);
  36. if(ans!=99) putchar(ans+'0');
  37. else printf("NO ANSWER!");
  38. return 0;
  39. }
Success #stdin #stdout 0s 4492KB
stdin
abcdefgh 12345678
abcd efgh
efgh 1234
efgh 5678
stdout
NO ANSWER!