3 条题解

  • 1
    @ 2024-5-27 16:54:38

    正则大法好

    import re
    
    # 读取求救者的人数
    n = int(input())
    
    # 初始化 st 数组用于存储每个求救信号 'sos' 出现次数对应的求救者名字
    st = [0] * 1000
    for i in range(1000):
        st[i] = []
    
    # 初始化变量 mx 用于记录最大的 'sos' 出现次数
    mx = 0
    for i in range(n):
        name = input()  # 读取名字
        sig = input()   # 读取求救信号
        # 使用正则表达式查找重叠的 'sos',计算出现的次数
        # 正则表达式 r'(?=(sos))' 的含义:
        #   '(?=...)' 是正向前瞻断言,它会查找紧接着条件表达式的位置
        #   '(sos)' 是要匹配的内容,写在前瞻中意味着搜索从每个字符开始检查是否接下来的字符构成 'sos'
        # 这样做将允许正则找到所有的重叠的 'sos' 模式
        num = len(re.findall(r'(?=(sos))', sig))
        # 把所有出现次数为 num 的求救者名字存储起来
        st[num].append(name)
        # 更新最大次数
        mx = max(mx, num)
    
    # 输出所有出现次数最多的求救者的名字
    for name in st[mx]:
        print(name, end=' ')
    
    print()
    # 输出最大次数
    print(mx)
    
    • @ 2024-5-28 9:06:26

      这道题最好的解法

    • @ 2024-6-4 9:33:52

      虽然但是正则本质也是O(n)O(n)遍历匹配

  • 0
    @ 2024-5-29 19:26:54
    def fin(s):
        cnt = 0
        for i in range(2, len(s)):
            if (s[i-2:i+1]=="sos"):
                cnt += 1
        return cnt
    
    p = int(input())
    kkksc03 = 0 ; chen_zhe=[]
    for i in range(p):
        name = input()
        cnt = fin(input())
        if (kkksc03<cnt):
            chen_zhe = [name]
            kkksc03 = cnt
        elif (kkksc03==cnt):
            chen_zhe += [name]
    
    
    if (kkksc03):
       for n in chen_zhe:
           print(n,end=" ")
       print("\n",kkksc03,sep="")
    
    • 0
      @ 2024-5-28 9:13:35

      重叠子串匹配中,效率较高的一个方案是使用KMP算法 (Knuth-Morris-Pratt算法)

      这是我随手写的一个比较史的解决方案,不是该题的答案,仅供参考:

      Python

      from typing import List
      
      def kmp_table(pattern: str) -> List[int]:
          table: List[int] = [0] * len(pattern)
          j: int = 0
          for i in range(1, len(pattern)):
              while j > 0 and pattern[i] != pattern[j]:
                  j = table[j - 1]
              if pattern[i] == pattern[j]:
                  j += 1
                  table[i] = j
          return table
      
      def kmp_search(text: str, pattern: str) -> List[int]:
          match_positions: List[int] = []
          if not text or not pattern:
              return match_positions
          table: List[int] = kmp_table(pattern)
          j: int = 0
          for i in range(len(text)):
              while j > 0 and text[i] != pattern[j]:
                  j = table[j - 1]
              if text[i] == pattern[j]:
                  if j == len(pattern) - 1:
                      match_positions.append(i - j)
                      j = table[j]
                  else:
                      j += 1
          return match_positions
      
      text: str = "sososos"
      pattern: str = "sos"
      matches: List[int] = kmp_search(text, pattern)
      print(matches)  # Output: [0, 2, 4]
      
      • @ 2024-6-4 9:30:13

        完整代码,删去了类型注解,(因为有人坚信其让我的代码变得难懂)

        Python

        from collections import defaultdict
        
        def kmp_table(pattern):
            table = [0] * len(pattern)
            j = 0
            for i in range(1, len(pattern)):
                while j > 0 and pattern[i] != pattern[j]:
                    j = table[j - 1]
                if pattern[i] == pattern[j]:
                    j += 1
                    table[i] = j
            return table
        
        def kmp_search(text, pattern):
            match_number = 0
            if not text or not pattern:
                return match_number
            table = kmp_table(pattern)
            j = 0
            for i in range(len(text)):
                while j > 0 and text[i] != pattern[j]:
                    j = table[j - 1]
                if text[i] == pattern[j]:
                    if j == len(pattern) - 1:
                        match_number += 1
                        j = table[j]
                    else:
                        j += 1
            return match_number
        
        n = int(input())
        
        max_count = 0
        emergency_seekers = defaultdict(list)
        
        for _ in range(n):
            name = input()
            signal = input()
            count = kmp_search(signal, 'sos')
            emergency_seekers[count].append(name)
            max_count = max(max_count, count)
        
        print(' '.join(emergency_seekers[max_count]))
        print(max_count)
        
    • 1

    信息

    ID
    932
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    842
    已通过
    173
    上传者