• 校招真题练习014 万万没想到之聪明的编辑(头条)


    万万没想到之聪明的编辑

    题目描述
    我叫王大锤,是一家出版社的编辑。我发现一个发现拼写错误的捷径:

    1. 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
    2. 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
    3. 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC

    请听题:请实现大锤的自动校对程序

    输入描述:
    第一行包括一个数字N,表示本次用例包括多少个待校验的字符串。
    后面跟随N行,每行为一个待校验的字符串。

    输出描述:
    N行,每行包括一个被修复后的字符串。

     1 N = int(input())
     2 for x in range(N):
     3     string = input().strip()
     4     n = len(string)
     5     temp = ''
     6     samecount = 0
     7     i = 0
     8     while i < n:
     9         if i == n - 1:
    10             temp += string[i]
    11             i += 1
    12         elif i + 1 < n and string[i] != string[i + 1]:
    13             temp += string[i]
    14             i += 1
    15         else:#s[i] == s[i+1]
    16             j = i + 1
    17             temp += string[i] + string[j]
    18             base = string[i]
    19             while j < n and base == string[j]:
    20                 j += 1
    21             #s[j] is different from s[i]
    22             if j == n:
    23                 break
    24             else:
    25                 temp += string[j]
    26                 base2 = string[j]
    27                 k = j + 1
    28                 while k < n and base2 == string[k]:
    29                     k += 1
    30                 i = k
    31     print(temp)

    算法思路:滑动窗口。

    在窗口内部“捕捉”以下两种类型的子串:

    1. 连续3个(含)以上相同的字符。处理办法:只保留2个字符。

    2. 连续2个字符之后,出现的连续2个(含)以上的相同字符。处理办法:只保留1个字符。

    捕捉完这两种之后,进行窗口向右平移(更新i值)。

    注意判断边界条件不要越界。

  • 相关阅读:
    小技巧与阅读感悟
    Android常见漏洞介绍(1):Intent Scheme URL 攻击
    利用Mallet工具自动挖掘文本Topic
    Xie Tao的几个小建议PPT:关于读Paper,如何与他人交流,赶Deadline
    Python解析DNS数据包
    iOS 横竖屏适配 笔记
    多工程联编,cocopods的使用
    Call to unavailable function 'system': not available on iOS
    xxx referenced from: xxx in xxx.o
    YYModel学习总结YYClassInfo(1)
  • 原文地址:https://www.cnblogs.com/asenyang/p/11217896.html
Copyright © 2020-2023  润新知