我的任务是在 C++ 中将队列实现为一个类。在向量类的构造函数中,传递了元素类型,例如vector <int> a. 我怎样才能将元素类型传递给我的类构造函数,比如MyQueue <int> a?
主页
/
user-439500
Null's questions
有一个任务:
最初,我用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 抱歉,我无法提供该任务的链接。
有几种完全不同的尝试来解决这个问题:
我首先使用 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 次测试中摔倒了。显然,在这两种解决方案中,我忘记考虑的一些小事都存在错误。你能告诉我需要修复/更换什么吗?
有这样一个任务:
我试着这样做:
#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;
}
她没有通过一项测试。我认为完整的搜索需要很多时间。请告诉我如何以另一种方式解决此问题或改进此问题。
我为它写了代码。结果是这样的:
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 次测试时它崩溃了。告诉我出了什么事?
我是这样解决的。我们有两个数字:左边框的索引和右边框的索引。首先,我们从左到右将数字相加。如果总和等于给定的,那么我们移动两个边界。如果得到的总和小于给定的总和,那么我们移动右边界。如果结果总和更大,那么我们移动左边框
#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 秒。请建议如何优化此问题。








