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