fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int isPossible(string s1, string s2,int k)
  4. {
  5. int cnt=0;
  6. for(int i=0;i<s2.size();i++)
  7. {
  8. int j=i;
  9. while(s1[j]!=s2[i]) j+=1;
  10. while(i<j)
  11. {
  12. swap(s1[j],s1[j-1]);
  13. cnt+=1;
  14. j-=1;
  15. }
  16.  
  17. }
  18. return cnt;
  19. }
  20. bool isSubsequence(string x,string y)
  21. {
  22. int j=0;
  23. for (int i=0;i<x.size() and j<y.size();i++)
  24. {
  25. if (y[j]==x[i]) j+=1;
  26. }
  27. return(j==y.size());
  28. }
  29. int main()
  30. {
  31. int n,m,k;
  32. cin>>n>>m>>k;
  33. assert(n>=1 and n<=10 and m>=1 and m<=10 and k>=0 and k<=1000);
  34. string S,T,copy_S;
  35. cin>>S>>T;
  36. assert(S.size()==n and T.size()==m);
  37. copy_S=S;
  38. sort(S.begin(),S.end());
  39. int ans=0;
  40. do
  41. {
  42. if(isSubsequence(S,T))
  43. {
  44. int x=isPossible(S,copy_S,k);
  45. if(x<=k) ans++;
  46. }
  47. }while(next_permutation(S.begin(),S.end()));
  48. cout<<ans<<'\n';
  49. }
  50.  
Success #stdin #stdout 0s 5624KB
stdin
2 1 2
ab
a
stdout
2