Files
exercises/dsa/linear_list/dsa_0201_dictionary.cpp
T
2026-09-09 13:38:11 +08:00

84 lines
2.1 KiB
C++

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
vector<string> words;
string deleted_char(const string& word, const int idx) {
assert(0 <= idx && idx < word.size());
return word.substr(0, idx) + word.substr(idx + 1);
}
vector<string> query(const string& s) {
vector<string> results;
for (string word : words) {
switch (static_cast<int>(word.size()) - static_cast<int>(s.size())) {
case 1: { // 需要删
for (int i = 0; i < word.size(); i++) {
if (word[i] != s[i]) {
if (deleted_char(word, i) == s) {
results.push_back(word);
break;
}
}
}
break;
}
case -1: {
for (int i = 0; i < s.size(); i++) {
if (word[i] != s[i]) {
if (deleted_char(s, i) == word) {
results.push_back(word);
break;
}
}
}
break;
}
case 0: {
if (word == s) { return vector{word}; }
int diff_count = 0;
for (int i = 0; i < s.size(); i++) {
if (word[i] != s[i]) {
diff_count++;
}
if (diff_count > 1) { break; }
}
if (diff_count == 1) {
results.push_back(word);
break;
}
break;
}
default: break;
}
}
return results;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
while (cin >> s && s[0] != '#') {
words.push_back(s);
}
while (cin >> s && s[0] != '#') {
vector<string> results = query(s);
if (!results.empty() && results[0] == s) {
cout << s << " is correct" << endl;
continue;
}
cout << s << ": ";
for (const string& result : results) {
cout << result << " ";
}
cout << endl;
}
return 0;
}