python实现pat 1071. Speech Patterns (25)

小灰灰 2022-06-04 02:25 243阅读 0赞

题目描述

People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.

Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

输入描述:
Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return ‘\n’. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

输出描述:
For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

输入例子:
Can1: “Can a can can a can? It can!”

输出例子:
can 5

解答1

先提取单词,再排序,最后统计。运算超时了。

  1. line=input()
  2. #Can1: "Can a can can a can? It can!"
  3. line=line.lower()
  4. words=[]
  5. bs = False
  6. be=False
  7. for i,c in enumerate(line):
  8. if (c>='a' and c<='z')or (c>='0' and c<='9'):
  9. if bs==False:
  10. s=i
  11. bs=True
  12. be=False
  13. else:
  14. if be==False:
  15. e=i
  16. be=True
  17. if bs==True and be==True:
  18. word=line[s:e]
  19. words.append(word)
  20. s=-1
  21. e=-1
  22. bs = False
  23. if s!= -1:
  24. word = line[s:]
  25. words.append(word)
  26. words.sort()
  27. word_max=None
  28. count_max=1
  29. count=1
  30. for i,word in enumerate(words[:-1]):
  31. if word==words[i+1] :
  32. count+=1
  33. if word == words[-2]:
  34. if count > count_max:
  35. count_max = count
  36. count = 1
  37. word_max = word
  38. elif count>count_max :
  39. count_max=count
  40. count=1
  41. word_max=word
  42. else:
  43. count = 1
  44. if count_max==1:
  45. word_max=words[0]
  46. print (word_max,count_max)

超时了。
解答1

解答2

用了字典,在提取单词的同时统计频次。但还是超时了,并且估计超得更多了。

  1. line=input()
  2. line=line.lower()
  3. #print (line)
  4. words={}
  5. bs = False
  6. be=False
  7. for i,c in enumerate(line):
  8. if (c>='a' and c<='z')or (c>='0' and c<='9'):
  9. if bs==False:
  10. s=i
  11. bs=True
  12. be=False
  13. else:
  14. if be==False:
  15. e=i
  16. be=True
  17. if bs==True and be==True:
  18. word=line[s:e]
  19. if word in words:
  20. words[word]+=1
  21. else:
  22. words[word]=1
  23. s=-1
  24. e=-1
  25. bs = False
  26. if s!= -1:
  27. word = line[s:]
  28. if word in words:
  29. words[word] += 1
  30. else:
  31. words[word] = 1
  32. count_max=0
  33. word_len=0
  34. word_max=None
  35. for word in words:
  36. if words[word]>count_max or( words[word]==count_max and word <word_max):
  37. count_max=words[word]
  38. word_max=word
  39. print (word_max,count_max)

解答2

发表评论

表情:
评论列表 (有 0 条评论,243人围观)

还没有评论,来说两句吧...

相关阅读