0

我有一个简单的 python 代码,它不断在CodeChef在线法官上显示 NZEC 错误。该代码用于问题GRANAMA

厨师学会了一种比较两种食谱的新技术。配方包含按处理时间递增顺序排列的成分列表。成分由字母“a”-“z”表示。食谱中的第 i 个字母表示第 i 个成分。一种成分可以在食谱中多次使用。

该技术如下。通过比较它们各自的列表来比较两个食谱。如果两个配方中使用的成分集相同,并且每种成分
在它们中的使用次数相同(处理顺序无关紧要),
则它们被声明为格拉纳玛配方。(“granama”是 Chef 中“相似”的意思。)Chef 采用了他昨天发明的两个食谱。他想用这种技术比较它们。不幸的是,厨师忘记记录每种成分在食谱中使用的次数。他只比较了成分,但没有比较它们
频率。更准确地说,如果没有在一个食谱中使用且未在另一个食谱中使用的成分,则 Chef 将两个食谱视为格拉纳玛。您的任务是报告 Chef 是否正确分类了这两个食谱(作为 granama 或非 granama),尽管他忘记了跟踪频率。

 Input

 The first line of the input contains a single integer T denoting the number of test 
 cases. The description for T test cases follows. Each test case consists of a single 
 line containing two space-separated strings R and S denoting the two recipes.

 Output

 For each test case, output a single line containing "YES" (quotes for clarity) if Chef 
 correctly classified the two recipes as granama or not granama. Otherwise, output a 
 single line containing "NO" (quotes for clarity) if Chef declared two recipes as 
 granama when they actually are not.

 Constraints

 1 ≤ T ≤ 100
 1 ≤ |R|, |S| ≤ 1000
 Example

 Input:

 3
 alex axle
 paradise diapers
 alice bob

 Output:

 YES 
 NO
 YES

 Explanation:

 Example case 1: Chef declared them as granama recipes. They are actually granama 
 because the sets of ingredients and the number of times each ingredient has been used 
 are equal. The Chef got it right!
 Example case 2: Chef declared them as granama recipes because both sets of ingredients 
 are equal. But they are NOT granama since ingredient 'a' has been used twice in the 
 first recipe but only once in the second. The Chef was incorrect!
 Example case 3: Chef declare them as not granama. They are not granama as the sets of 
 ingredients are different. Hence, the Chef was right!

这是代码:

k=int(raw_input())
for n in range(k):
    recipes=raw_input().split(' ')
    w1=recipes[0]
    w2=recipes[1]
    for i in w1:
        if i in w1:
            w1=w1.replace(i,'')
            w2=w2.replace(i,'')

    if w1=='' and w2=='':
        dict1={}
        dict2={}
        for i in recipes[0]:
            if i in dict1:
                dict1[i]+=1
            else:
                dict1[i]=1
        for i in recipes[1]:
            if i in dict2:
                dict2[i]+=1
            else:
                dict2[i]=1
        flag=True
        for i in dict1:
            if not dict1[i]==dict2[i]:
                print 'NO'
                flag=False
                break
        if flag:
            print 'YES'

    else:
        print 'YES'  
4

1 回答 1

1

我相信错误就在这里-

for i in w1:
    if i in w2:    # You were checking again in 'w1'
        w1=w1.replace(i,'')
        w2=w2.replace(i,'')

这应该可以解决 NZEC 问题。你可以在这里看到我提交的解决方案。运行时间为 0.23 秒。

有很多空间可以使这段代码紧凑。让我挑选一些你的代码片段并展示如何 -

w1=recipes[0]
    w2=recipes[1]
    for i in w1:
        if i in w2:
            w1=w1.replace(i,'')
            w2=w2.replace(i,'')

if w1=='' and w2=='':

Python有一套数据结构,在这种情况下可以派上用场。

if set(recipes[0]) == set(recipes[1]):    # Check for equality between two sets

要查看两个配方之间每个字符的频率是否匹配,您不需要字典。您可以简单地比较已排序的字符串。

if sorted(recipes[0]) == sorted(recipes[1])

所以,这 30 行代码可以用 7 行代替。

    if set(recipes[0]) == set(recipes[1]):
        if sorted(recipes[0]) == sorted(recipes[1]):
            print 'YES'
        else:
            print 'NO'
    else:
        print 'YES'

基于上述代码,我以稍微不同的方式输入。它在 0.09 秒内运行。

于 2013-01-07T03:47:59.717 回答