簡體   English   中英

運行時錯誤(NZEC)Python

[英]Runtime error (NZEC) Python

我正在嘗試以下問題

    Master Shifu is organizing a quiz for Po. The quiz consists of N multiple-choice 
questions, conveniently numbered 0 through N-1. For each question there are three possible answers labeled A, B, and C. For each question, exactly one of the three possible
answers is correct. You are given the information on correct answers as a String answers.
More precisely, answers[i] is the correct answer to the i-th question. This is of course unknown to Po.


    Po did not know the answer to any of the quiz questions. Being desperate, he with some
help from Tigress,Mantis,Crane,Viper and Monkey he got the answer script. However, the only
thing he could obtain was aggregate information on the answers that will be used. More 
precisely, Po now knows how many questions have the answer A, how many have the answer B, 
and how many have the answer C.


    When taking the quiz, Po will be given the questions one at a time, in the same order
that is used in answers. For each of the questions, he will use his intuition to pick one 
of the answers that are still possible. After he picks the answer for a question, Master 
Shifu will tell him the correct answer for that question. Po has noticed that sometimes he 
can rule out some of the answers when answering a question. Your task is to help Po by 
telling him the number of options he will choose from when answering i-th question.

Constraints:

1<=N<=50

Each character of answers will be 'A', 'B', or 'C'.





Input

First line will contain the number of test cases, T. Then T lines follow each containg the string of answers.

Output

Output an array of N numbers, where i-th element will tell the number of options Po will choose from when answering i-th question.

我在python中編寫的代碼是

from collections import Counter
t = int(raw_input())
for i in range(t):
    po = []
    s = raw_input()
    a = Counter(s)
    options = 3
    for x in s:
        na = a['A']
        nb = a['B']
        nc = a['C']
        if na == 0 or nb == 0 or nc == 0:
            options = 2
        if (na == 0 and nb == 0) or (nb == 0 and nc == 0) or (nc == 0 and na == 0):
            options = 1
        if (na == 0 and nb == 0 and nc == 0):
            options = 0
        po.append(options)
        a[x] -= 1
    out = ""
    for x in po:
        out += str(x) + " "
    print out

我在spoj門戶網站上運行代碼時遇到運行時錯誤(NZEC),但我在ideone(http://ideone.com/WseoQ)上糾正了輸出

Input:
4
AAAAA
AAABBB
CAAAAAC
BBCA

我懷疑由於輸入中有多余的空格,您正在使用NZEC。 您可以嘗試立即獲取輸入,並用空格將其標記化。

from collections import Counter
import sys
tokenizedInput = sys.stdin.read().split()
t = int(tokenizedInput[0])
for i in range(t):
    po = []
    s = tokenizedInput[i+1]

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM