Codeforces Round #388 (Div. 2), problem: (C) Voting Solution in C/C++
#include<stdio.h> typedef unsigned u; char S[222222],D[222222]; int main() { u l,i,ds,rs,dq,rq; ds=rs=dq=rq=0; scanf("%u%s",&l,S); for(i=-1;++i<l;) { if(S[i]=='D')++ds; if(S[i]=='R')++rs; } for(;ds&&rs;) { for(i=-1;++i<l;)if(!D[i]) { if(S[i]=='D') { if(dq){--dq;--ds;D[i]=1;continue;} ++rq; } if(S[i]=='R') { if(rq){--rq;--rs;D[i]=1;continue;} ++dq; } } } if(ds)putchar('D'); if(rs)putchar('R'); return 0; }