a53

minim lexicografic

a53
Jan 9th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. ifstream f("minlex.in");
  6. ofstream g("minlex.out");
  7.  
  8. char S[100001];
  9. int N;
  10.  
  11. bool compara(char *S, int i, int j)
  12. {
  13. int k;
  14. for (k=0;k<N;k++)
  15. {
  16. if (S[i]<S[j]) return false;
  17. if (S[i]>S[j]) return true;
  18. i=(i+1)%N;
  19. j=(j+1)%N;
  20. }
  21. return true;
  22. }
  23.  
  24. int minlex(char *S)
  25. {
  26. int poz=0, i;
  27. for (i=1;i<N;i++)
  28. if (compara(S,poz,i)) poz=i;
  29. return poz;
  30. }
  31.  
  32. int main()
  33. {
  34. f>>S;
  35. f.close();
  36. N=strlen(S);
  37. g<<minlex(S)<<'\n';
  38. g.close();
  39. return 0;
  40. }
Add Comment
Please, Sign In to add comment