fork download
  1. // HackerRank
  2. // Algorithms > Strings > Bigger is Greater
  3. // Written by Travis Holt - 20150714
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <algorithm> // std::reverse
  8.  
  9. using namespace std;
  10.  
  11. int findSuffix(string &s) {
  12. int suffixLength = 1;
  13. int i = s.length() - 1;
  14. while (s[i] <= s[i -1]) {
  15. suffixLength++;
  16. i--;
  17. }
  18. return suffixLength;
  19. }
  20.  
  21. int main() {
  22. /*-------------------------------------------------------------------------
  23. Constraints:
  24. Number of test cases (T): 1 <= T <= 10^5 (long)
  25. Word (W): 1 <= W <= 100
  26. Word (W) will only contain lower-case latin letters
  27. -------------------------------------------------------------------------*/
  28.  
  29. // Get number of test cases
  30. long testCases;
  31. cin >> testCases;
  32. cin.ignore();
  33.  
  34. // Process test cases
  35. for (long tc = 0; tc < testCases; tc++) {
  36. // Get input word
  37. string word;
  38. getline(cin, word);
  39.  
  40. int suffixIndex = findSuffix(word);
  41. if (suffixIndex >= word.length()) {
  42. // Don't process string, no solution.
  43. cout << "no solution" << endl;
  44. } else {
  45. cout << "Suffix length = " << findSuffix(word) << endl;
  46. }
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 5284KB
stdin
5
ab
bb
hefg
dhck
dkhc
stdout
Suffix length = 1
no solution
Suffix length = 1
Suffix length = 1
Suffix length = 3