RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-439500

Null's questions

Martin Hope
Null
Asked: 2022-06-06 21:49:11 +0000 UTC

创建自己的容器类的实例时如何将元素类型传递给“<>”

  • 0

我的任务是在 C++ 中将队列实现为一个类。在向量类的构造函数中,传递了元素类型,例如vector <int> a. 我怎样才能将元素类型传递给我的类构造函数,比如MyQueue <int> a?

c++
  • 1 个回答
  • 10 Views
Martin Hope
Null
Asked: 2022-05-10 07:10:48 +0000 UTC

奥赛任务优化

  • 8

有一个任务:

在此处输入图像描述

在此处输入图像描述

最初,我用python编写代码,结果是这样的:

from array import array


def string_xor(a: str, b: str):
    """
    Проверка на схожесть строк
    3 - одинаковые
    2 - 1 различие
    1 - больше 2 отличий
    """
    k = len(a)
    if k == len(b):
        a1 = array('u', a)
        b1 = array('u', b)
        errors = 0
        for i in range(k):
            if a1[i] != b1[i]:
                errors += 1
        if errors > 1:
            return 1
        elif errors == 1:
            return 2
        elif errors == 0:
            return 3
    return 1


n, m = map(int, input().split())
words = sorted(input() for _ in range(n))
for _ in range(m):
    word = input()
    one_diff = []  # Список слов, которые расходятся ровно на одну букву
    for cur in words:
        ans = string_xor(word, cur)
        if ans == 3:
            print(cur)
            break
        elif ans == 2:
            one_diff.append(cur)
    else:
        if one_diff:
            print(min(one_diff))
        else:
            print('?')

但它在其中一项时间测试中崩溃。然后我重写了它的优点,认为它会有所帮助。

#include<iostream>
#include "vector"
#include "algorithm"
using namespace std;

int check(string a, string b) {
    int k = a.size();
    if (k == b.size()) {
        int errors(0);
        for (int i = 0; i < k; i++)
            errors += a[i] != b[i];
        if (errors == 0)
            return 0;
        else if (errors == 1)
            return 1;
        return 2;
    }
    return 2;
}

int main() {
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    string word;
    vector <string> words(n), one_diff;
    for (string &el : words)
        cin >> el;
    sort(words.begin(), words.end());
    for (int i = 0; i < m; i++) {
        cin >> word;
        bool is_break(false);
        for (const string &cur : words) {
            int d = check(cur, word);
            if (d == 0) {
                cout << cur << "\n";
                is_break = true;
                break;
            }
            else if (d == 1)
                one_diff.push_back(cur);
        }
        if (!is_break) {
            if (!one_diff.empty()) {
                sort(one_diff.begin(), one_diff.end());
                cout << one_diff[0] << "\n";
                one_diff.clear();
            }
            else
                cout << "?" << "\n";
        }
    }
}

但它并没有帮助,落在同样的考验。告诉我这里需要更改什么,以便程序及时适应。

我做了一个字符串长度的字典,有一个时间是1.033,它变成了1.09代码:

from array import array


def string_xor(a: str, b: str):
    k = len(a)
    if k == len(b):
        errors = 0
        for a1, b1 in zip(a, b):
            errors += a1 != b1
        if errors > 1:
            return 1
        elif errors == 1:
            return 2
        elif errors == 0:
            return 3
    return 1


n, m = map(int, input().split())
d = {}
for _ in range(n):
    word = input()
    z = len(word)
    d[z] = d.get(z, [word]) + [word]
for i in d.keys():
    d[i].sort()
for _ in range(m):
    word = input()
    one_diff = set()
    try:
        for cur in d[len(word)]:
            ans = string_xor(word, cur)
            if ans == 3:
                print(cur)
                break
            elif ans == 2:
                one_diff.add(cur)
        else:
            if one_diff:
                print(min(one_diff))
            else:
                print('?')
    except KeyError:
        print('?')

PS 代码及时崩溃...
PPS 抱歉,我无法提供该任务的链接。

python
  • 4 个回答
  • 10 Views
Martin Hope
Null
Asked: 2022-04-29 02:53:50 +0000 UTC

帮助改善问题

  • 2

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

在此处输入图像描述

在此处输入图像描述

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

我首先使用 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 个回答
  • 10 Views
Martin Hope
Null
Asked: 2022-04-28 03:28:28 +0000 UTC

任务优化

  • 2

有这样一个任务:

在此处输入图像描述

我试着这样做:

#include <iostream>
using namespace std;

bool check(int a, int b, int c){
    return a < b + c && b < a + c && c < a + b;
}

int main() {
    int a, b, c, d, count = 0;
    cin >> a >> b >> c >> d;
    for (int x = a; x <= b; x++){
        for (int y = b; y <= c; y++){
            for (int z = c; z <= d; z++){
                count += check(x, y, z);
            }
        }
    }
    cout << count;
}

她没有通过一项测试。我认为完整的搜索需要很多时间。请告诉我如何以另一种方式解决此问题或改进此问题。

c++
  • 1 个回答
  • 10 Views
Martin Hope
Null
Asked: 2022-04-19 23:59:28 +0000 UTC

任务未通过测试

  • 0

有这样一个任务: 在此处输入图像描述

它有一个细分: 在此处输入图像描述

我为它写了代码。结果是这样的:

n = int(input())
cords = [list(map(int, input().split())) for _ in range(n)]
cords.sort(key=lambda x: x[1])
total = None
for col in range(1, n + 1):
    s = 0
    for row in range(1, n + 1):
        cur = cords[row - 1]
        cur_x = cur[1]
        cur_y = cur[0]
        s += abs(cur_x - col)
        s += abs(cur_y - row)
    if total is None:
        total = s
    elif s < total:
        total = s
print(total)

但是在第 10 次测试时它崩溃了。告诉我出了什么事?

python
  • 1 个回答
  • 10 Views
Martin Hope
Null
Asked: 2022-04-17 17:27:52 +0000 UTC

C++ 代码优化/奥林匹克问题

  • 1

有这样一个任务:在此处输入图像描述

我是这样解决的。我们有两个数字:左边框的索引和右边框的索引。首先,我们从左到右将数字相加。如果总和等于给定的,那么我们移动两个边界。如果得到的总和小于给定的总和,那么我们移动右边界。如果结果总和更大,那么我们移动左边框

#include <iostream>
#include "vector"
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    int n, k, i, count = 0;
    cin >> n >> k;
    vector<int> autos(n);
    for (auto &h: autos) {
        cin >> h;
    }
    int l = 0, r = 0;
    while (r != n) {
        long long sum = 0;
        for (i = l; i <= r; i++) {
            sum += autos[i];
        }
        if (sum == k) {
            count++;
            l++;
            r++;
        }
        else if (sum < k) {
            r++;
        }
        else if (sum > k) {
            l++;
        }
        sum = 0;
    }
    cout << count;
}

但它没有通过最后一次测试,即 0.081 秒。请建议如何优化此问题。

c++
  • 1 个回答
  • 10 Views

Sidebar

Stats

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

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 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