RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1276168
Accepted
Null
Null
Asked:2022-04-29 02:53:50 +0000 UTC2022-04-29 02:53:50 +0000 UTC 2022-04-29 02:53:50 +0000 UTC

帮助改善问题

  • 772

我正在尝试解决这个问题: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

有几种完全不同的尝试来解决这个问题:

我首先使用 itertools.groupby 在 python 中编写

from itertools import groupby
from sys import stdin
keys = input().split()
repeat_counts = '-c' in keys
only_repeats = '-d' in keys
ignore_reg = '-i' in keys
only_uniq = '-u' in keys
args = {}
if ignore_reg:
    args = {'key': str.lower}

for st in groupby(map(str.strip, stdin.readlines()), **args):
    counts = sum(1 for _ in st[1])
    if only_repeats and only_uniq:
        continue
    if repeat_counts:
        if only_repeats:
            if counts > 1:
                print(counts, st[0])
        elif only_uniq:
            if counts == 1:
                print(1, st[0])
        else:
            print(counts, st[0])
    else:
        if only_repeats:
            if counts > 1:
                print(st[0])
        elif only_uniq:
            if counts == 1:
                print(st[0])
        else:
            print(st[0])

该任务通过了 11 次测试。

然后我决定用 C++ 编写任何我能用笨拙的方法吃的代码。也就是计算重复次数。

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;
bool
    repeat_count = false,
    only_repeats = false,
    without_reg = false,
    only_unique = false;

char change_case(char c) {
    if (isupper(c))
        return tolower(c);
    return c;
}

string pass(string st) {
    return st;
}

string lower(string st) {
    transform(st.begin(), st.end(), st.begin(), change_case);
    return st;
}
void answer(int count, string old){
    if (only_repeats) {
        if (count > 1) {
            if (repeat_count) {
                cout << count << " " << old << '\n';
            } else
            {
                cout << old << "\n";
            }
        }

    } else if (only_unique) {
        if (count == 1) {
            if (repeat_count) {
                cout << 1 << " " << old << "\n";
            }
            else {
                cout << old << "\n";
            }
        }
    }
    else {
        if (repeat_count) {
            cout << count << " " << old << "\n";
        }
        else {
            cout << old << "\n";
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    string keys, key, st;
    getline(cin, keys);
    istringstream ss(keys);
    while (ss >> key) {
        st = key.c_str();
        if (st == "-c") {
            repeat_count = true;
        } else if (st == "-d") {
            only_repeats = true;
        } else if (st == "-i") {
            without_reg = true;
        } else if (st == "-u") {
            only_unique = true;
        }
    }
    if (only_unique and only_repeats)
        return 0;
    string old, cur;
    cin >> old;
    int count = 1;
    auto func = pass;
    if (without_reg) {
        func = lower;
    }
    while (cin >> cur) {
        if (func(cur) == func(old)) {
            count++;
        }
        else {
            answer(count, old);
            count = 1;
            old = cur;
        }
    }
    answer(count, old);

}

任务到了10分。然后我想在12月解决它,在我看来这是一个更可靠的方法。她看起来像这样:

#include "iostream"
#include "algorithm"
#include "sstream"
#include "deque"
using namespace std;

char change_case(char c) {
    return isupper(c) ? tolower(c) : c;
}

string pass(string st) {
    return st;
}

string lower(string st) {
    transform(st.begin(), st.end(), st.begin(), change_case);
    return st;
}

bool
        repeat_count = false,
        only_repeats = false,
        without_reg = false,
        only_unique = false;

auto func = pass;

void init() {
    string keys, key, temp;
    getline(cin, keys);
    istringstream ss(keys);
    while (ss >> key) {
        temp = key.c_str();
        if (temp == "-c") {
            repeat_count = true;
        }
        else if (temp == "-d") {
            only_repeats = true;
        }
        else if (temp == "-i") {
            without_reg = true;
        }
        else if (temp == "-u") {
            only_unique = true;
        }
    }
    if (without_reg)
        func = lower;
}

void answer(const deque <string> &a){
    string out;
    int sz = a.size();
    if (repeat_count){
        if (only_unique){
            if (sz == 1){
                cout << sz << " " << a.front() << "\n";
            }
        }
        else if (only_repeats){
            if (sz != 1){
                cout << sz << " " << a.front() << "\n";
            }
        }
        else{
            cout << sz << " " << a.front() << "\n";
        }
    }
    else{
        if (only_unique){
            if (sz == 1){
                cout << a.front() << "\n";
            }
        }
        else if (only_repeats){
            if (sz != 1){
                cout << a.front() << "\n";
            }
        }
        else{
            cout << a.front() << "\n";
        }
    }
}

int main() {
    init();
    if (only_unique and only_repeats)
        return 0;
    string cur;
    cin >> cur;
    deque <string> st = {cur};
    while (cin >> cur) {
        if (func(cur) == func(st.front())){
            st.push_back(cur);
        }
        else{
            answer(st);
            st.clear();
            st.push_back(cur);
        }
    }
    answer(st);
}

然后她在第 10 次测试中摔倒了。显然,在这两种解决方案中,我忘记考虑的一些小事都存在错误。你能告诉我需要修复/更换什么吗?

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Stanislav Volodarskiy
    2022-04-29T07:07:43Z2022-04-29T07:07:43Z

    itertools经过一些修改,您的版本运行良好。可以这样纠正:

    for st in groupby(map(str.strip, stdin.readlines()), **args):
        value = next(st[1]) # получаем первое значение из группы, печатать надо его
        counts = 1 + sum(1 for _ in st[1])
    

    我在其他地方梳理了一下:

    import itertools
    import sys
    
    keys = next(sys.stdin).split()
    ignore_case = '-i' in keys
    print_count = '-c' in keys
    unique_only = '-u' in keys
    duplicates_only = '-d' in keys
    
    if duplicates_only:
        if unique_only:
            check = lambda count: False
        else:
            check = lambda count: count > 1
    else:
        if unique_only:
            check = lambda count: count == 1
        else:
            check = lambda count: True
    
    if print_count:
        emit = lambda count, value: print(count, value, end='')
    else:
        emit = lambda count, value: print(value, end='')
    
    key = str.lower if ignore_case else None
    for k, g in itertools.groupby(sys.stdin, key=key):
        value = next(g)
        count = 1 + sum(1 for _ in g)
        if check(count):
            emit(count, value)
    

    您的 C++ 变体读取的是单词,而不是行。他们需要认真重新设计。循环混乱等等。

    • 3

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5