我认为我的算法是正确的(可能做得非常糟糕)但我在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;
先谢谢。
答案 0 :(得分:-1)
您没有考虑输入数据结束的事实。 sys.stdin
是一个类似于对象的文件,需要一个EOF字符来标记结尾。由于它不存在,python尝试在没有数据时读取更多数据并且您收到错误。
您应该使用input
或raw_input
代替sys.stdin
。例如,在上面的代码中,替换2 nd 和3 rd 行(假设您使用Python 2.7)
t = input("")
同时用
替换第一个while
中的第一行
a = input("")