有几种完全不同的尝试来解决这个问题:
我首先使用 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 次测试中摔倒了。显然,在这两种解决方案中,我忘记考虑的一些小事都存在错误。你能告诉我需要修复/更换什么吗?



itertools经过一些修改,您的版本运行良好。可以这样纠正:我在其他地方梳理了一下:
您的 C++ 变体读取的是单词,而不是行。他们需要认真重新设计。循环混乱等等。