找出所有的能够由其它的两个字符串组成的字符串.
可以先建起字典树,然后对每一个单词 枚举拆分。
#include <cstring>
#include <iostream>
#include <cstdio>
using namespace std;
char s[50010][50];
struct node {
int num;
node * next[26];
} ;
node *root;
void trie(){
root = new node ;
root-> num = 0;
for(int i=0;i<26;i++){
root->next[i] = NULL;
}
}// 插入
void insert(char *ss){
node *cur = root,*t ;
for(int i=0;ss[i];i++){
int idx = ss[i] - 'a' ;
if(!cur->next[idx]){
t= new node ;
t-> num = 0 ;
for(int j = 0;j<26;j++){
t ->next[j] = NULL;
}
cur->next[idx] = t ;
}
cur = cur -> next[idx];
}
cur ->num = 1;
} // 查找
bool search(char *ss){
node * cur = root ;
for(int i =0;ss[i];i++){
int idx = ss[i]-'a';
if(!cur->next[idx]) return false ;
cur= cur->next[idx];
}
if(cur->num)
return true;
return false ;
}
char tmp1[50];
char tmp2[50];
int main(){
trie();
int tot = 0 ;
while(~scanf("%s",s[++tot])){
insert(s[tot]);
}
for(int i=1;i<=tot;i++){
int len = strlen(s[i]);
for(int j =1;j<len ;j++){
memset(tmp1 ,'\0',sizeof(tmp1));
memset(tmp2,'\0',sizeof(tmp2));
strncpy(tmp1 , s[i],j);
strncpy(tmp2 , s[i]+j,len-j);
if(search(tmp1)&&search(tmp2)){
printf("%s\n",s[i]);
break;
}
}
}
return 0;
}