SPOJ RAFANOLE中的错误答案

时间:2013-11-08 18:02:20

标签: python python-2.7

我认为我的算法是正确的(可能做得非常糟糕),我在ideone.com上得到了理想的输出。但是在SPOJ中,它继续说“运行时错误NZEC”。然后我添加了“sys.exit(0)”。 然后NZEC错误停止了。 但现在它显示“错误答案”

请建议进行一些更改以使其正确。

以下是问题的链接:

http://www.spoj.com/problems/RAFANOLE/

这是我的代码:

def main():
    import sys;
    inp=sys.stdin.read().split("\n");
    t=int(inp[0]);
    i=1;
    g_N=dict();
    g_D=dict();
    t_N=dict();
    t_D=dict();
    while(i<t+1):
        a=inp[i].split();
        game_N =0;
        curr_N =0;
        game_D =0;
        curr_D =0;
        tie_N=0;
        tie_D=0;
        k=0;
        n=len(a);
        while ((k<n)and((game_N<6)or(game_D<6))):
            if(a[k]=='N'):
                curr_N = curr_N +1;
                if(curr_N==4 and curr_D < 3):
                    game_N=game_N +1;
                    curr_N=0;
                    curr_D=0;
                if(curr_N==3 and curr_D ==3) :
                    while(a[k]!=a[k+1]):
                        k=k+1;
                    if(a[k]=='N'):
                        game_N=game_N+1;
                        curr_N=0;
                        curr_D=0;
            if(a[k]=='D'):
                curr_D=curr_D+1;
                if(curr_D==4 and curr_N<3):
                  game_D=game_D+1;
                  curr_D=0;
                  curr_N=0;
                if(curr_N==3 and curr_D ==3) :
                   while(a[k]!=a[k+1]):
                       k=k+1;
                   if(a[k]=='D'):
                       game_D=game_D+1;
                       curr_N=0;
                       curr_D=0;
            k=k+1;
        if((game_N==6 and game_D==5 ) or (game_N==5 and game_D==6)):
            curr_N=0;
            curr_D=0;
            while(game_N != 7 and game_D != 7):
                if (a[k]=='N'):
                    curr_N=curr_N+1;
                if(a[k]=='D'):
                    curr_D=curr_D+1;
                if(curr_N==4 and curr_D<3):
                    game_N=game_N+1;
                    curr_D=0;
                    curr_N=0;
                if(curr_D==4 and curr_N<3):
                    game_D=game_D+1;
                    curr_N=0;
                    curr_D=0;
        if (game_N==6 and game_D ==6):
            while (1):
                if(a[k]=='N'):
                    tie_N=tie_N+1;
                if(a[k]=='D'):
                    tie_D=tie_D+1;
                if(((tie_D==7 or tie_N==7) and (abs(tie_D-tie_N)>=2)) or ((tie_D>7 or tie_N>7)and(abs(tie_D-tie_N)>=2))):
                    k=k+1;
                    break;
                k=k+1;

        if(tie_N>tie_D):
            game_N=game_N+1;
        elif(tie_N<tie_D) :
            game_D=game_D+1;
        g_N[i]=game_N;
        g_D[i]=game_D;
        t_N[i]=tie_N;
        t_D[i]=tie_D;

        i=i+1;
    i=1;
    while(i<t+1):        
        if(g_N[i]>g_D[i]):
            if(t_N[i]==0 and t_D[i] ==0):
                print ("N %d" % g_N[i]);
                print ("D %d" % g_D[i]);
                print "\t";
            else :
                print ("N %d(%d-%d)"%(g_N[i],t_N[i],t_D[i]));
                print ("D %d" % g_D[i]);
        else:
            if(t_N[i]==0 and t_D[i] ==0):
                print ("D %d" % g_D[i]);
                print ("N %d" % g_N[i]);
            else :
                print ("D %d(%d-%d)"%(g_D[i],t_D[i],t_N[i]));
                print ("N %d" % g_N[i]);
        i=i+1;
    sys.exit(0)
if __name__=="__main__":
    main();

1 个答案:

答案 0 :(得分:0)

你检查

if((game_N==6 and game_D==5 ) or (game_N==5 and game_D==6)):
        curr_N=0;
        curr_D=0;
        while(game_N != 7 and game_D != 7):

这是错误。 if的条件可能后跟game_N=6,game_d=D,在你的场景中没有考虑,如果任何一个是6而另一个是5,你迭代直到任何1达到7,从而使得条件从不发生一个。