暴力解法
时间复杂度:O(n^2)
空间复杂度:O(n)
测试用例有问题,AC=75%??? are you sure?
#include <iostream> #include <vector> #include <string> using namespace std; //判断两个字符串是否为包含关系 bool isIncludeString(string s0, string s1) { int len0 = s0.size(); int len1 = s1.size(); bool flag; for (int i = 0; i <= len0; i++) { for (int j = 0; j < len1; j++) { if (s0[i] == s1[j]) { return true; } } } return false; } int main() { vector<string> vec; string s; string c; while (cin >> s) { if (s == "end") { break; } vec.push_back(s); } int res = 0; int maxn = 0; for (int i = 0; i < vec.size() - 1; i++) { for (int j = i + 1; j < vec.size(); j++) { if (isIncludeString(vec[i], vec[j])) { continue; } else { //更新最大值 maxn = max(res, (int)(vec[i].size() * vec[j].size())); } } } cout << maxn << endl; return 0; }