C - You Are Given a WASD-string... CodeForces - 1202C


#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+100; char s[maxn]; /* This character can not just add, because there will be, you add a character The minimum shift to the left to the right a bit, but led to the far right to the right, and not worth the candle First, we consider the vertical direction, we assume it in this position 0, then he moved Location is a difference of point 1, we added a W in front of a position, then the coordinates of all points behind him are a plus If you add s in front of a position, then the coordinates of all points behind him will be minus one, You can be reduced to ensure that, when you add a, not be followed by a maximum value, minimum value and can not have the front Minus one when the latter can not have a minimum and a maximum value front can not have, or may not So find the kind of, you can add a situation that can be added to reduce, not increase it will not change the kind of Because the vertical and horizontal situation, we consider only one side We must be clear to you plus one, plus one behind it all, you have to subtract a minus one behind it all vertical If it reaches the maximum number of times and the minimum number of times 1, when the last time a maximum value in the first minimum of the foregoing, we simply increase the value of the back of a minimum value to reduce the And you change the position of the other, or make larger value, or the value will change 2, the last time when the minimum occurs before the maximum value at the first, as long as the minimum back minus one, then the value becomes too small The other two cases changed and not changed as, or larger cause * / Int len; The annual maxlast, maxfirst, minlast, minfirst; int work(char c,char c1) { maxlast = = = maxfirst minlast minfirst = 0 ; ll max_=0,min_=0; ll y=0; for(int i=0; i<len; i++) { if(s[i]==c) y++; else if(s[i]==c1) y--; if(max_<y) { maxlast = maxfirst = i; max_=y; } else if(y==max_) { maxlast = i; } if(y<min_) { minlast = minfirst = i; Min_ = the y; } else if (y == min_) { minlast = i; } } return (max_-min_ + 1 ); } int main () { int t; scanf("%d",&t); while(t--) { scanf("%s",s); len = strlen (s); int flag = 0 , flag1 = 0 ; ll n=work('W','S'); if((maxlast<minfirst||maxfirst>minlast)&&n>2) flag=1; ll m=work('A','D'); if((maxlast<minfirst||maxfirst>minlast)&&m>2) flag1 = 1 ; ll ans=n*m; if(flag&&flag1)ans=min(n*(m-1),m*(n-1)); else if(flag)ans=(n-1)*m; else if(flag1) years = (m- 1 ) * n; printf("%lld\n",ans); } return 0; }

 

Guess you like

Origin www.cnblogs.com/zhangzhenjun/p/11708116.html