2012-12-15 3 views
0

나는 온라인 판사 CodeChef에 NZEC 오류를 계속 표시하는 간단한 파이썬 코드가 있습니다. 문제의 코드는 GRANAMA입니다.이 코드에서 Nzec 런타임 오류가 있습니까?

요리사는 두 가지 조리법을 비교하는 새로운 기술을 배웠습니다. 조리법에는 처리 할 시간이 증가하는 순서로 재료 목록이 포함되어 있습니다. 성분은 문자 'a'- 'z'로 표시됩니다. 조리법의 i 번째 문자는 i 번째 성분 인 을 나타냅니다. 재료는 레시피에서 여러 번 사용할 수 있습니다.

기술은 다음과 같습니다. 각각의 목록을 비교하여 두 가지 조리법을 비교하십시오. 두 요리법에 사용 된 재료 세트가 동일하고 각 재료가
인 경우 (처리 순서는 중요하지 않음)
그라나다 리그 요리법으로 선언됩니다. ("granama"는 "유사"에 대한 Chef-ian 단어입니다.) 요리사는 어제 발명 한 두 가지 조리법을 사용했습니다. 그는 기술을 사용하여 그들을 비교하기를 원했습니다. 불행히도 Chef는 각 성분이 조리법에 사용 된 횟수를 추적하는 것을 잊어 버렸습니다. 그는 재료만을 비교했으나 주파수는
개가 아닙니다. 더 정확하게, 요리사는 성분이 하나의 조리법에서 사용되고 다른 조리법에서는 사용되지 않으면 두 가지 조리법을 그라나다로 간주합니다. 당신은 주파수를 추적하는 것을 잊었지만 요리사가 정확하게 두 가지 요리법 ( granama 또는 not granama)을 분류했는지 여부를보고합니다.

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

이는 NZEC 문제를 해결해야한다 - 나는 오류가 여기에있다 생각

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' 

답변

1

:

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! 

여기에 코드입니다. 제출 한 솔루션은 here 이상입니다. 런타임은 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=='': 

파이썬은 이와 같은 경우에 매우 편리 올 수있는 set data structure을 가지고 - 내가 당신의 코드 조각의 일부를 픽업하는 방법을 보여 드리겠습니다.

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' 

code에서 위 입력 내용과 약간 다른 방식으로 입력했습니다. 그것은 0.09s에서 실행됩니다.

관련 문제