Title Link: http://codeforces.com/gym/100507/attachments
----------------------------------------------------------------------------
When I first saw it, it felt like an interval $dp$. However the complexity has been stuck in $o (n^3) $ optimization does not come down
And then I tried, and some of the greedy ones hung up on the bigger data.
Repeatedly thinking of uppercase letters and corresponding lowercase letters match to think of the previously done brace matching
Only the left and right relationship between the uppercase and lowercase letters is unlimited.
So you can use a stack to help greedy
If the new letters are currently added, they can be matched directly to the top of the stack.
Otherwise, it will be left on top of the stack (to prevent the matching line from being limited to match the letters between the matching lines).
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intN =10010;7 CharS[n];8 intSta[n];9 intMa[n], ans[n], num[n];Ten intN, Len, top; One intMain () A { -scanf"%d%s", &n, S +1); -Len = n <<1; the for(inti =1; I <= Len; ++i) - if(!top | | ABS (int) S[sta[top-1]]-s[i])! = +) -sta[top++] =i; - Else + { -Ma[i] = sta[top-1]; +Ma[sta[top-1]] =i; A--top; at } - if(top) - { -Puts"Impossible"); - return 0; - } in for(inti =1; I <= Len; ++i) - if(S[i] <'a') toNum[i] = num[i-1]; + Else -Num[i] = num[i-1] +1; the for(inti =1; I <= Len; ++i) * if(S[i] <'a') $printf"%d", Num[ma[i]]);Panax Notoginseng}
Gym 100507h-pair:normal and paranormal