首页 > 试题广场 >

万万没想到之聪明的编辑

[编程题]万万没想到之聪明的编辑
  • 热度指数:54993 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:

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

我特喵是个天才!我在蓝翔学过挖掘机和程序设计,按照这个原理写了一个自动校对器,工作效率从此起飞。用不了多久,我就会出任CEO,当上董事长,迎娶白富美,走上人生巅峰,想想都有点小激动呢!
……
万万没想到,我被开除了,临走时老板对我说: “做人做事要兢兢业业、勤勤恳恳、本本分分,人要是行,干一行行一行。一行行行行行;要是不行,干一行不行一行,一行不行行行不行。” 我现在整个人红红火火恍恍惚惚的……

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

数据范围: ,每个用例的字符串长度满足

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

后面跟随N行,每行为一个待校验的字符串。


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

输入

2
helloo
wooooooow

输出

hello
woow
示例2

输入

1
nowcoder

输出

nowcoder
def corrected_spelling(s):
    stack = []
    for char in s:
        if len(stack) >= 2 and char == stack[-1] and char == stack[-2]:
            continue
        elif len(stack) >= 3 and char == stack[-1] and stack[-2] == stack[-3]:
            continue
        else:
            stack.append(char)
    return "".join(stack)
编辑于 2024-04-12 21:09:01 回复(0)
s = re.sub(r'(\w)\1{2,}', r'\1\1', s)
s = re.sub(r'(\w)\1{2}(\w)\2{2}', r'\1\1\2', s)
发表于 2023-11-20 17:29:54 回复(0)

用正则替换

import re
N = int(input())
for i in range(N):
    strlist = input()
    strlist = re.sub(r"([a-z])\1{2,}", r"\1\1", strlist)
    strlist = re.sub(r"([a-z])\1{1}([a-z])\2{1}", r"\1\1\2", strlist)
    print(strlist)
发表于 2022-03-06 18:58:12 回复(0)
# -*- coding: utf-8 -*- """ @Time    : 2022/1/27 22:21 @Author  : CARL_AIB_SHEN @File    : DealAABB.py 三个同样的字母连载一起,一定是拼写错误,去掉一个。 例如helllo ->hello 两对一样的字母 AABB 连载一起,一定是拼写错误哦 改成AAB 例如helloo  -> hello 上面规则有限从左到右匹配,如果AABBCC 改成AABCC 即可。 """ # eeefid   eehhlid   ehhbboie  heeeloe   heehhaai heehhaaddi  haxieee   haxieehh # haxieehhff  hello haxieehhffdd haxieeehhhfff    haxieeehhhfffheehhaai from typing import List
tempNum = int(input())
targetStringArr: List[str] = [] # print("----", tempNum) for i in range(0, tempNum):
    targetStringArr.append(input()) # print("----", tempNum) # print(targetStringArr) for targetString in targetStringArr:
    letter1 = ""  letter2 = ""  letter3 = ""  letter4 = ""  wrongPointIndex = -1  continuePointIndex = 0  currentPointIndex = 0   #  print(targetString.__len__())  #  print("输入题目的单词:", targetString)   while currentPointIndex < targetString.__len__() - 3: for i in range(continuePointIndex, targetString.__len__() - 2):
            currentPointIndex = i
            letter1 = targetString[i]
            letter2 = targetString[i + 1] if letter1 == letter2:
                letter3 = targetString[i + 2] if letter1 == letter3:
                    wrongPointIndex: int = i + 2  # print("找到一个AAA,序号", wrongPointIndex)  if wrongPointIndex != -1: break  else: if targetString.__len__() > i + 3:
                        letter4 = targetString[i + 3] # print("34:", letter3, letter4)  if letter3 == letter4:
                            wrongPointIndex = i + 2  # print("找到AABB,序号", wrongPointIndex)  break  # 进行处理  # print("wrongPointIndex,", wrongPointIndex)  if wrongPointIndex != -1:
            targetString = targetString[0: wrongPointIndex] + targetString[wrongPointIndex + 1:targetString.__len__()] # print("修改的:", targetString)  # 记录一下,已经查找到的位置  continuePointIndex = wrongPointIndex - 2  # 重置判断标志位  wrongPointIndex = -1  # print("修改continuePointIndex:", continuePointIndex)  # print("最终结果:", targetString)  print(targetString)

发表于 2022-01-28 23:09:52 回复(0)
N = int(input().strip())
l = []
for i in range(N):
    l.append(input().strip())
for i in range(N):
    for j in range(len(l[i])-2):
        if(l[i][j]==l[i][j+1]==l[i][j+2]):
            l[i] = l[i][:j] + ' ' + l[i][j+1:]
    l[i] = l[i].replace(' ', '')
for i in range(N):
    for j in range(len(l[i])-3):
        if(l[i][j]==l[i][j+1] and l[i][j+2]==l[i][j+3]):
            l[i] = l[i][:j+2] + ' ' + l[i][j+3:]
    l[i] = l[i].replace(' ', '')
for i in range(N):
    print(l[i])
发表于 2021-05-10 13:58:07 回复(0)
whw_另造判断输入法

n = int(input())

res = []
for i in range(n):
    t = str(input())
    ax = ''
    for e in t:
        if len(ax) < 2:
            ax+=e 
            continue
        if len(ax) >=2 and e ==ax[-1] and e==ax[-2]:
            continue
        if len(ax) >=3 and e==ax[-1] and ax[-2]==ax[-3]:
            continue
        ax+=e 
    print(ax)



编辑于 2020-11-10 16:58:16 回复(0)
n = int(input())
while n > 0:
    s = list(input())
    l = len(s)
    i = 0
    while i <= l:
        if (i+3)<=l and s[i] == s[i+1] and s[i] == s[i+2]:
            del s[i]
            l -= 1
            continue
        elif (i+4)<=l and s[i] == s[i+1] and s[i+2] == s[i+3]:
            del s[i+2]
            l -= 1
            continue
        i += 1
    print(''.join(s))
    n -= 1

发表于 2020-06-17 17:14:58 回复(0)
import sys
 
lines = []
for line in sys.stdin:
    lines.append(line[:-1])
 
     
def fun(line):
    for index in range(1, len(line)):
        if line[index] == line[index - 1]:
            if line[index] == line[index + 1:index + 2]:
                return fun(line[:index + 1] + line[index + 2:])
            elif line[index + 1:index + 2] == line[index + 2:index + 3]:
                return fun(line[:index + 2] + line[index + 3:])
            else:
                continue
    return line
 
 
for line in lines[1:]:
    print(fun(line)) 


不明白为什么会越界,通过了9%,测后面那个15项的用例中的一个超长的字符串,也没问题,谁能解答一下,懵了
发表于 2020-04-11 00:00:38 回复(0)
import re


def test(ma):
    return ma.group()[1:]

def test2(ma):
    return ma.group()[0:len(ma.group())-1]

for i in range(int(input())):
    a = input()
    
    while re.search(r"(.)\1\1", a):
        a = re.sub(r"(.)\1\1", test, a)

    while re.search(r"(.)\1(.)\2", a):
        a = re.sub(r"(.)\1(.)\2", test2, a)

    print(a)

发表于 2020-03-03 00:00:30 回复(0)
之前由于版本问题,代码在python2运行不了,在python3里正常运行。
def main():
    n = input()
    if n == 0&nbs***bsp;n == None:
        return None
    for i in range(int(n)):
        s=input()
        j=1
        while(j<=len(s)-1):
            if j > 2:
                if (s[j]==s[j-1]==s[j-2]&nbs***bsp;(s[j]==s[j-1] and s[j-2]==s[j-3])):
                    s=s[:j]+s[j+1:]
                else:
                    j = j + 1
            elif (j > 1 and s[j]==s[j-1]==s[j-2]):
                    s=s[:j]+s[j+1:]
            else:
                j = j + 1
        print(s)
if __name__ == '__main__':
    main()


编辑于 2020-03-02 17:01:28 回复(2)
"""
我可真笨啊,做了好久也坐了好久。。。
"""
import sys                       #前两行是标准输入语句,做公司真题时候需要这么写的,不然过不去
for s in list(sys.stdin)[1:]:
    try:                       #这里的try好像没啥用,写上保险点
        res = []             #接下来就是常规思路了,搞一个列表然后把待检测字符串一个字符一个字符的喂进去
        for s0 in s.strip():
            if len(res) < 2:
                res.append(s0)
                continue
            if len(res) >= 2:
                if s0 == res[-1] and s0 == res[-2]:
                    continue
            if len(res) >= 3:
                if s0 == res[-1] and res[-2] == res[-3]:
                    continue
            res.append(s0)
        print("".join(res))
    except:
        pass

发表于 2019-07-13 09:14:56 回复(2)

使用递归...不如在线算法简洁

def make_right(word):
    if len(word) <= 2:
        return word
    elif len(word) == 3:
        if word[0] == word[1] and word[0] == word[2]:
            return word[0:2]
        else:
            return word
    elif len(word) == 4:
        if word[0] == word[1] and word[2] == word[3]:
            return word[0:3]
        else:
            return word
    for i in range(len(word) - 3):
        if word[i] == word[i+1] and word[i] == word[i+2]:   # AAA
            del word[i]
            return make_right(word)
        if word[i] == word[i+1] and word[i+2] == word[i+3]:  # AABB
            del word[i+2]
            return make_right(word)
    i = len(word) - 3
    if word[i] == word[i+1] and word[i] == word[i+2]:
        del word[i]
    return word
n = int(input())
for i in range(n):
    word = input()
    right_word = make_right(list(word))
    print(''.join(right_word)) 
编辑于 2019-07-06 19:42:38 回复(0)