zoukankan      html  css  js  c++  java
  • 字节真题:万万没想到之聪明的编辑

    牛客链接:https://www.nowcoder.com/question/next?pid=16516564&qid=362292&tid=49703321

    我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:
     
    1. 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
    2. 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
    3. 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC

    解题思路:比较简单,一个队列记录结果,另外用两个变量判断当前元素是否加入队列,但也用了40分钟,一开始没想清楚,用栈去了,,,qaq

    #include<iostream>
    #include<queue>
    #include<vector>
    using namespace std;
    
    string getAloneRes(string init) {
        string res;
        if (init.size() == 0) return res;
        queue<char> sque;
        string::iterator it = init.begin();
        sque.push(*it);
        int pre_idx = 0, cur_idx = 1;
        for (++it; it != init.end(); ++it) {
            if (sque.back() == *it) {   // 和队尾元素相同
                /*if (pre_idx == 2 && cur_idx == 1) {
                    continue;
                }
                else if (cur_idx == 2) {
                    continue;
                }*/
                // 11oo 和 111可以合并为一个判断 
                if (pre_idx == 2 || cur_idx == 2) continue;
                else {
                    cur_idx++;sque.push(*it);
                }
            }
            else {            // 和栈顶元素不同
                pre_idx = cur_idx;
                cur_idx = 1;
                sque.push(*it);
            }
        }
        // 将得到的结果出栈(队列啊)
        while (!sque.empty()) {
            res += sque.front();sque.pop();
        }
        return res;
    }
    
    vector<string> getRes(int n, vector<string> init) {
        vector<string> res;
        int str_count = 0;   // 正在处理第几个字符串
        while (str_count < n) {
            res.push_back(getAloneRes(init[str_count++]));
        }
        return res;
    }
    
    int main() {
        int n;
        cin >> n;
        vector<string> init;
        for (int i = 0; i < n; ++i) {
            string tmp;
            cin >> tmp;
            init.push_back(tmp);
        }
        vector<string> res = getRes(n, init);
        for (int i = 0; i < n; ++i) {
            cout << res[i] << endl;
        }
        return 0;
    }
    心之所愿,永不相忘
  • 相关阅读:
    maven报错【Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of】
    Srping框架中使用@query注解实现复杂查询
    FreeMarker自定义TemplateDirectiveModel
    SpringMVC和Freemarker整合,带自定义标签的使用方法
    关于FreeMarker自定义TemplateDirectiveModel
    滑块验证码【插件待研究】
    注册页面 注册码【欠缺较多 待完善】
    IO流-文件的写入和读取
    Date、String、Calendar相互转化
    Runtime类
  • 原文地址:https://www.cnblogs.com/zgll/p/15500937.html
Copyright © 2011-2022 走看看