虽然说n的范围很大,不得遍历,但还是尝试交了个遍历上去,果不其然的TLE了。
题中所给的“输入已按字典序从小到大排列”条件,其实是在暗示你可以按照输入的单词遍历(而非按照其组合遍历)。仅需要加一个判重map即可。也可以使用set.insert(但是时间更长)
#include <iostream>
#include <cmath>
#include <ctime>
#include <climits>
#include <iomanip>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <iterator>
#include <queue>
using namespace std;
map<string, int> maptable;
map<string, int> ans;
vector<string> dict;
int main()
{
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
string s, tmp1, tmp2;
while (cin >> s)
{
dict.push_back(s);
maptable[s] = 1;
}
for (int i = 0; i < dict.size(); i++)
for (int j = 0; j < dict[i].length() - 1; j++) //No possibility to get repeated combinations.
{
tmp1 = dict[i].substr(0, j + 1);
if (!maptable[tmp1])
continue;
tmp2 = dict[i].substr(j + 1);
if (!maptable[tmp2])
continue;
if (!ans.count(dict[i]))
cout << dict[i] << endl;
ans[dict[i]] == 1;
}
//end of program
return 0;
}