字节跳动面试题01万万没想到之聪明的编辑

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:

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

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

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

参考代码:

n = int(input())
while n > 0:
    res = []
    chars = input() 
    for char in chars:
        if len(res) < 2 :
            res.append(char)
            continue
        if len(res) >= 2:
            if char == res[-1] and char == res[-2]:    # 错误1:三个字母连在一起
                continue
        if len(res) >=3:
            if char == res[-1] and res[-2] == res[-3]:   # 错误2: lloo型,过滤第二个o
                continue                                # 第三点从左到右匹配其实就是for循环的作用之一
        res.append(char)
    print("".join(res))
    n-=1

C++

#include <iostream> 
using namespace std;

int main()
{
    int n;
    cin >> n;
    while (n--) {
        int state = 0;
        char last = '\0'; 
        char x; 

        while (cin >> x) {
            switch (state) {
                case 0:
                    if (x == last)
                        state = 1;
                    break;
                case 1:
                    if (x == last)
                        state = 2;
                    else
                        state = 3;
                    break;
                case 2:
                    if (x != last)
                        state = 3;
                    break;
                case 3:
                    if (x == last)
                        state = 4;
                    else
                        state = 0;
                    break;
                case 4:
                    if (x != last)
                        state = 0;
            }
            if(state!=2&&state!=4)
            {
            	cout<<x;
            }
            last=x;
         }
         cout<<endl;
     }
}

猜你喜欢

转载自blog.csdn.net/weixin_44611644/article/details/95243159
今日推荐