|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <list> |
| 4 | +#include <string> |
| 5 | +#include <unordered_map> |
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +typedef long long ll; |
| 9 | + |
| 10 | +//Basic Libraries and definitions^ |
| 11 | + |
| 12 | +int t; //TestCases |
| 13 | +int x, n, current; |
| 14 | +int mod = 1000000000; //10^9 |
| 15 | +std::vector<int> vec; |
| 16 | +std::string s; |
| 17 | + |
| 18 | +void solve(std::string s){ |
| 19 | + current = 0; |
| 20 | + std::string prev = s; |
| 21 | + next_permutation(s.begin(),s.end()); |
| 22 | +/*This next permutation is a small cheatsheet that c++ had for this problem |
| 23 | +We are working with next permutations in order to find the next largest string |
| 24 | +in lexigraphical order and the function does it for us |
| 25 | +*/ |
| 26 | + |
| 27 | + for(int c = 0; c < s.length(); ++c){ |
| 28 | + int aci = s[c]; |
| 29 | + int prevAci = prev[c]; |
| 30 | +//We needed to compare the original string to the next permutation string to |
| 31 | +//find extra test cases: |
| 32 | + if(prevAci > aci){ |
| 33 | + std::cout << "no answer" << std::endl; |
| 34 | + c = s.length(); |
| 35 | +//If the permutated string has an element less than the old string without |
| 36 | +//Finding a larger number in a spot before it, then it'll be less |
| 37 | + } |
| 38 | + else if(prevAci < aci){ |
| 39 | + std::cout << s << std::endl; |
| 40 | + c = s.length(); |
| 41 | +//If the string already has a number larger than the old string, because it's |
| 42 | +//guarenteed to permutated to the smallest extent, we're good to go |
| 43 | + } |
| 44 | + else if(prevAci == aci){ |
| 45 | + ++current; |
| 46 | + } |
| 47 | +//These if statements up and below look to see if the string has all the same letters |
| 48 | + |
| 49 | + if(current == s.length()){ |
| 50 | + std::cout << "no answer" << std::endl; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +int main(){ |
| 57 | + //Runs the test cases with input |
| 58 | + std::cin >> t; |
| 59 | + for(int i = 0; i < t; ++i){ |
| 60 | + std::cin >> s; |
| 61 | + solve(s); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +//Time complexity: O(N) |
0 commit comments