1 条题解
-
0
解题思路:
- 对于每个单词,先提取首尾字母,将中间字母排序作为其特征标识。
- 对字典中的每个单词建立特征标识与单词的映射关系。
- 对于输入的句子,将其中每个单词按照同样规则提取特征标识,然后根据映射关系,统计每个单词可能的替换数量。
- 利用组合数学原理,将每个单词的替换数量相乘,得到最终能形成的句子数量。 实现步骤:
- 初始化字典映射:遍历字典中的每个单词,提取特征标识并建立与单词的映射关系。
- 处理输入句子:对于每个输入句子,拆分单词,提取每个单词的特征标识,查找映射关系获取可能的替换单词数量。
- 计算句子数量:将每个单词的替换数量相乘,得到该句子能形成的句子数量。
- 输出结果:按照输出格式要求,输出每个测试用例中每个句子的计算结果。 代码:
#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <map> #include <algorithm> #include <cstring> using namespace std; string encode_word(const string &word) { if (word.size() <= 2) return word; string encoded = word.substr(0, 1); string middle = word.substr(1, word.size()-2); sort(middle.begin(), middle.end()); encoded += middle + word.substr(word.size()-1); return encoded; } map<string, int> preprocess_dictionary(const vector<string> &dict) { map<string, int> encoded_counts; for (size_t i = 0; i < dict.size(); ++i) { string encoded = encode_word(dict[i]); encoded_counts[encoded]++; } return encoded_counts; } int count_sentences(const string &sentence, const map<string, int> &encoded_counts) { istringstream iss(sentence); string word; int total = 1; while (iss >> word) { string encoded = encode_word(word); map<string, int>::const_iterator it = encoded_counts.find(encoded); if (it == encoded_counts.end()) { return 0; } total *= it->second; } return total; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int num_test_cases; cin >> num_test_cases; for (int tc = 1; tc <= num_test_cases; ++tc) { int n; cin >> n; vector<string> dictionary(n); for (int i = 0; i < n; ++i) { cin >> dictionary[i]; } map<string, int> encoded_counts = preprocess_dictionary(dictionary); int m; cin >> m; cin.ignore(); cout << "Scenario #" << tc << ":\n"; for (int i = 0; i < m; ++i) { string sentence; getline(cin, sentence); int count = count_sentences(sentence, encoded_counts); cout << count << "\n"; } cout << "\n"; } return 0; }
} std::printf("%d\n", result); } std::printf("\n"); } return 0;
}
- 1
信息
- ID
- 1494
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 3
- 已通过
- 1
- 上传者