fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int fact(int x)
  4. {
  5. if(x==1 || 0)
  6. return 1;
  7. return x*(x-1);
  8. }
  9.  
  10.  
  11. int findRank(string &A) {
  12. string b=A;
  13. sort(A.begin(),A.end());
  14. int l=A.length();
  15. int pos=1;
  16. for(int i=0;i<l-1;i++)
  17. {
  18. char ch=b[i];
  19. int nless=A.find(to_string(ch));
  20. cout<<nless<<" ";
  21. pos+=(nless*fact(l-i-1));
  22. cout<<pos<<" ";
  23. A.erase(nless,1);
  24. }
  25. return pos;
  26.  
  27. }
  28. int main()
  29. {
  30. string a="abc";
  31. cout<<findRank(a);
  32. return 0;
  33. }
  34.  
Runtime error #stdin #stdout #stderr 0s 4408KB
stdin
view
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::erase: __pos (which is 18446744073709551615) > this->size() (which is 3)