我认为我的算法是正确的(可能做得很糟糕)但我在 ideone.com 中得到了所需的输出。但在 SPOJ 中,它一直说“运行时错误 NZEC”。请提出一些更改以使其正确。
这是问题的链接:
http://www.spoj.com/problems/RAFANOLE/
这是我的代码:
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+2):
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 ==6):
break;
if((game_N==6 and game_D==5 ) or (game_N==5 and game_D==6)):
curr_N=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;
if(curr_D==4 and curr_N<3):
game_D=game_D+1;
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))):
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+2;
i=1;
while(i<t+2):
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+2;
提前致谢。