#32510: python tle,想問用list一定會tle嗎


Alan990118 (Alan990118)

學校 : 國立新竹高級中學
編號 : 144336
來源 : [140.138.54.192]
最後登入時間 :
2023-03-28 20:01:11
a291. nAnB problem | From: [140.138.242.154] | 發表日期 : 2022-10-17 23:53

from sys import stdin
password=[]
output = ''  
while True:
    try:
        password=list(map(int,stdin.readline().split()))#輸入密碼
        if password == []: password=list(map(int,stdin.readline().split()))
        backup_password=password.copy()#複製密碼
        n=int(stdin.readline())  
        for k in range(n):
           
            p=q=0
            try_password=list(map(int,stdin.readline().split()))
            for t in range(4):#計算A
                if password[t]==try_password[t]:
                    p+=1
                    backup_password[t]=-1
                    try_password[t]=-1
            for t in backup_password:#計算B
                if t!=-1:
                    if t in try_password:
                        q+=1
                        try_password[try_password.index(t)]=-1
                   
            output+=f"{p}A{q}B\n"
    except:
        print(output)
        break
這題我計算A、B那邊改了很多次,想問這題是不是用list一定會超時?還有一定要用dict才能不超時嗎?
 
#32511: Re: python tle,想問用list一定會tle嗎


Alan990118 (Alan990118)

學校 : 國立新竹高級中學
編號 : 144336
來源 : [140.138.54.192]
最後登入時間 :
2023-03-28 20:01:11
a291. nAnB problem | From: [140.138.242.154] | 發表日期 : 2022-10-18 00:27

from sys import stdin
password=[]
output = ''  
while True:
    try:
        password=list(map(int,stdin.readline().split()))#輸入密碼
        if password == []: password=list(map(int,stdin.readline().split()))
        backup_password=password.copy()#複製密碼
        n=int(stdin.readline())  
        for k in range(n):
           
            p=q=0
            try_password=list(map(int,stdin.readline().split()))
            for t in range(4):#計算A
                if password[t]==try_password[t]:
                    p+=1
                    backup_password[t]=-1
                    try_password[t]=-1
            for t in backup_password:#計算B
                if t!=-1:
                    if t in try_password:
                        q+=1
                        try_password[try_password.index(t)]=-1
                   
            output+=f"{p}A{q}B\n"
    except:
        print(output)
        break
這題我計算A、B那邊改了很多次,想問這題是不是用list一定會超時?還有一定要用dict才能不超時嗎?
from sys import stdin
password=backup_password=[]
output = ''  
while True:
    try:
        password=list(map(int,stdin.readline().split()))
        if password == []: password=list(map(int,stdin.readline().split()))
        n=int(stdin.readline())  
        for k in range(n):
            backup_password=password.copy()
            p=q=0
            try_password=list(map(int,stdin.readline().split()))
            for t in range(4):
                if password[t]==try_password[t]:
                    p+=1
                    backup_password.pop(backup_password.index(password[t]))
                    try_password[t]=-1
            for t in backup_password:
                if t in try_password:
                    q+=1
                    try_password[try_password.index(t)]=-1
                   
            output+=f"{p}A{q}B\n"
    except:
        print(output)
        break
抱歉 上面那篇改到範例測資都過不了 這篇才正確的



 
#32521: Re: python tle,想問用list一定會tle嗎


asnewchien@gmail.com (david)

學校 : 不指定學校
編號 : 68108
來源 : [114.42.146.36]
最後登入時間 :
2024-05-16 22:25:39
a291. nAnB problem | From: [1.168.47.222] | 發表日期 : 2022-10-18 20:07

backup_password.index

你用 index 比較花時間。

最近伺服器又變慢了, 
python 的朋友解題要精打細算。

 
#32522: Re: python tle,想問用list一定會tle嗎


asnewchien@gmail.com (david)

學校 : 不指定學校
編號 : 68108
來源 : [114.42.146.36]
最後登入時間 :
2024-05-16 22:25:39
a291. nAnB problem | From: [1.168.47.222] | 發表日期 : 2022-10-18 20:16

不一定用了 dict 就會比較快,
因為只用了 0~9 你可以開一個 digit[10]
針對每組測資讀取之後
例如正確的密碼 1228 先統計為 [0, 1, 2, 0, 0, 0, 0, 0, 1, 0]
嘗試的密碼 6 7 8 9
digit[6] = 0 第一個 6 就可以略過,這樣才能省時間。

 

 
ZeroJudge Forum