[编程题]万万没想到之聪明的编辑

2 篇文章 0 订阅

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

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

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

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

#include <iostream>
using namespace std;

int main()
{
    int n;
    string str;
    
    cin>>n;
    while(n--)
    {
        cin >> str;
        int i,j, n = str.length();
        for(i=2, j=2; j<n; j++){
            if(str[i-2] == str[i-1] && str[i-1] == str[j]) 
                continue;
            if(i >= 3 && str[i-3] == str[i-2] && str[i-1] == str[j]) 
                continue;
            else str[i++] = str[j];
        }
        str = str.substr(0, i);
        cout << str << endl;
    }
}

scala感觉不如C/C++适合操作字符串。。。

import scala.io.StdIn
import scala.collection.mutable._
import util.control.Breaks._

object main {
  def main(args: Array[String]): Unit = {
    val n = StdIn.readInt()
    for (j <- 0 until n) {
      var newWord = ArrayBuffer[Char]()
      val temp = StdIn.readLine()
      for (i <- temp.indices) {
        newWord += temp(i)
      }

      var k = 2
      for (i <- 2 until newWord.length) {
        breakable {
          //依次判断两个规则
          if ((newWord(k - 2) == newWord(k - 1)) && (newWord(k - 1)) == newWord(i))
            break
          if ((k >= 3) && (newWord(k - 3) == newWord(k - 2)) && (newWord(k - 1) == newWord(i)))
            break
          newWord(k) = newWord(i)
          k += 1
        }
      }

      if (k < newWord.length)
        newWord(k) = '\0'

      breakable {
        for (i <- newWord.indices) {
          var ch = newWord(i)
          if (ch != '\0') {
            print(ch)
          } else {
            break()
          }
        }
      }

      println()
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值