-
Notifications
You must be signed in to change notification settings - Fork 643
/
20.7.cpp
50 lines (43 loc) · 1.24 KB
/
20.7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
#include "hash.h"
using namespace std;
Hash hash;
inline bool cmp(string s1, string s2){//按长度从大到小排
return s2.length() < s1.length();
}
bool MakeOfWords(string word, int length){
//cout<<"curr: "<<word<<endl;
int len = word.length();
//cout<<"len:"<<len<<endl;
if(len == 0) return true;
for(int i=1; i<=len; ++i){
if(i == length) return false;//取到原始串,即自身
string str = word.substr(0, i);
//cout<<str<<endl;
if(hash.find((char*)&str[0])){
if(MakeOfWords(word.substr(i), length))
return true;
}
}
return false;
}
void PrintLongestWord(string word[], int n){
for(int i=0; i<n; ++i)
hash.insert((char*)&word[i][0]);
sort(word, word+n, cmp);
for(int i=0; i<n; ++i){
if(MakeOfWords(word[i], word[i].length())){
cout<<"Longest Word: "<<word[i]<<endl;
return;
}
}
}
int main(){
string arr[] = {"test", "tester", "testertest", "testing",
"apple", "seattle", "banana", "batting", "ngcat",
"batti","bat", "testingtester", "testbattingcat"};
int len = 13;
PrintLongestWord(arr, len);
return 0;
}