// HackerRank
// Algorithms > Strings > Bigger is Greater
// Written by Travis Holt - 20150714
#include <iostream>
#include <string>
#include <algorithm> // std::reverse
using namespace std;
int findSuffix(string &s) {
int suffixLength = 1;
int i = s.length() - 1;
while (s[i] <= s[i -1]) {
suffixLength++;
i--;
}
return suffixLength;
}
int main() {
/*-------------------------------------------------------------------------
Constraints:
Number of test cases (T): 1 <= T <= 10^5 (long)
Word (W): 1 <= W <= 100
Word (W) will only contain lower-case latin letters
-------------------------------------------------------------------------*/
// Get number of test cases
long testCases;
cin >> testCases;
cin.ignore();
// Process test cases
for (long tc = 0; tc < testCases; tc++) {
// Get input word
string word;
getline(cin, word);
int suffixIndex = findSuffix(word);
if (suffixIndex >= word.length()) {
// Don't process string, no solution.
cout << "no solution" << endl;
} else {
cout << "Suffix length = " << findSuffix(word) << endl;
}
}
return 0;
}
Ly8gSGFja2VyUmFuawovLyBBbGdvcml0aG1zID4gU3RyaW5ncyA+IEJpZ2dlciBpcyBHcmVhdGVyCi8vIFdyaXR0ZW4gYnkgVHJhdmlzIEhvbHQgLSAyMDE1MDcxNAoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8YWxnb3JpdGhtPiAvLyBzdGQ6OnJldmVyc2UKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgZmluZFN1ZmZpeChzdHJpbmcgJnMpIHsKCWludCBzdWZmaXhMZW5ndGggPSAxOwoJaW50IGkgPSBzLmxlbmd0aCgpIC0gMTsKCXdoaWxlIChzW2ldIDw9IHNbaSAtMV0pIHsKCQlzdWZmaXhMZW5ndGgrKzsKCQlpLS07Cgl9CglyZXR1cm4gc3VmZml4TGVuZ3RoOwp9CgppbnQgbWFpbigpIHsKCS8qLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQoJQ29uc3RyYWludH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