1974D - Ingenuity-2 - CodeForces Solution


constructive algorithms implementation

Please click on ads to support us..

Python Code:

from collections import Counter,defaultdict
N=[0,1]
S=[0,-1]
E=[1,0]
W=[-1,0]
def jia(a,op):
    return [a[0]+op[0],a[1]+op[1]]
for i in range(int(input())):
    n=int(input())
    s=input()
    chu=[0,0]
    n="NO"
    if len(s)==2:
        if s[0]==s[1]:
            print("RH")
            continue 
        else:
            print("NO")
            continue 
    k=Counter(s)
    for i in s:
        chu=jia(chu,eval(i))
    if chu[0]%2!=0 or chu[1]%2!=0:
        print("NO")
    else:
        final=[chu[0]//2,chu[1]//2]
        res=[]
 
        count=defaultdict(lambda:0)
        if final[0]!=0:
            if final[0]<0:
                count["W"]=abs(final[0])
            else:
                count["E"]=abs(final[0])
        if final[1]!=0:
            if final[1]<0:
                count["S"]=abs(final[1])
            else:
                count["N"]=abs(final[1])
        if final==[0,0]:
            k=Counter(s)
            if (k["W"]>0 and k["E"]>0):
                count["W"]=1
                count["E"]=1
            elif (k["S"]>0 and k["N"]>0):
                count["S"]=1
                count["N"]=1
               for i in s:
            if count[i]!=0:
                count[i]-=1
                res.append("R")
            else:
                res.append("H")
        print("".join(res))


Comments

Submit
0 Comments
More Questions

1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes