Skip to content

Instantly share code, notes, and snippets.

@fferegrino
Created March 8, 2014 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fferegrino/9437546 to your computer and use it in GitHub Desktop.
Save fferegrino/9437546 to your computer and use it in GitHub Desktop.
2534 - Arrow Flips
#include <iostream>
#include <vector>
using namespace std;
int main(){
int tk, k, T, s, t, tmax, i , j;
char c;
cin >> T;
while(T--){
s = 0;
t = 0;
tmax = 0;
i = 0;
j = 0;
std::vector<char> a;
cin >> tk;
c = cin.get();
c = cin.get();
while(c != '\n' && c != 13){
s++;
a.insert(a.end(), c);
c = cin.get();
}
if(tk > 0){
k = tk;
while(i <= j && j < s){
while(k > 0 && j < s){
if(a[j] == '<'){
k--;
}
t++;
j++;
}
while(a[j] == '>' && j < s){
t++;
j++;
}
tmax = t > tmax ? t : tmax;
while(a[i] == '>'){
i++;
t--;
}
i++;
k++;
t--;
}
k = tk;
t = 0;
i = j = s-1;
while(i <= j && i >= 0){
while(k > 0 && i >= 0){
if(a[i] == '>'){
k--;
}
t++;
i--;
}
while(i >= 0 && a[i] == '<'){
t++;
i--;
}
tmax = t > tmax ? t : tmax;
while(a[j] == '<'){
j--;
t--;
}
j--;
k++;
t--;
}
} else {
while(i <= j && j < s){
while(a[j] == '>' && j < s){
t++;
j++;
}
while(a[j] == '<' && j < s){
j++;
i++;
}
tmax = t > tmax ? t : tmax;
t = 0;
}
t = 0;
i = j = s-1;
while(i <= j && i >= 0){
while(a[i] == '<' && i >= 0){
t++;
i--;
}
while(a[i] == '>' && i >= 0){
j--;
i--;
}
tmax = t > tmax ? t : tmax;
t = 0;
}
}
cout << tmax << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment