字典树模板 +枚举:对输入的每一个单词,记录下来,先插入字典树中,然后再对每一单词枚举成两半,再分别到字典树中查询,如果存在,则输出,注意,abcd----
a,bcd-----ab-cd,不要吧它输出两遍......这个题卡了一天多..... 衰 太钻死牛角尖了!!!!!!!!!!!!
#include<iostream>
#include<cstdio>#include<string.h>
using namespace std;
const int N=100010;
int n, m, cnt;
char s[N],dic[50100][30];
struct node
{
int p[26];
int flag;
} tree[N];
void init()
{
cnt = 1;
for(int i=0; i<N; i++) //初始化。。。
{
tree[i].flag = 0;
memset(tree[i].p, -1, sizeof(tree[i].p));
}
}
void insert(char *s) //插入某字符串。。。
{
int k=0;
//cout<<s<<"**************"<<endl;
while(*s)
{
if(tree[k].p[*s-'a']==-1)
tree[k].p[*s-'a'] = cnt++;
k = tree[k].p[*s-'a'];
s++;
}
tree[k].flag++;
}
int query(char *s) //查询某字符串的信息。。。
{
// cout<<s<<"****"<<endl;
int k=0;
while(*s)
{
if(tree[k].p[*s-'a']==-1)
return 0;
k = tree[k].p[*s-'a'];
s++;
}
return tree[k].flag;
}
int main()
{
char ch[30];
int sum=0,i,j,k,t;
init();//忘了初始化
while(gets(ch)&&ch[0])
{
strcpy(dic[sum++],ch);
insert(ch);
//cout<<ch<<"***"<<endl;
}
for(i=0;i<sum;i++)
{
char s1[30],s2[30];
int len=strlen(dic[i]);
for(j=0;j<=len-2;j++)
{
for(k=0;k<=j;k++)
s1[k]=dic[i][k];
s1[k]='\0';
for(t=0;k<=len-1;k++,t++)//错了一次
s2[t]=dic[i][k];
s2[t]='\0';
//cout<<s1<<"***"<<s2<<endl;
int a1=query(s1),a2=query(s2);
//cout<<"a1="<<a1<<"***a2="<<a2<<endl;
if(query(s1)&&query(s2))
{
printf("%s\n",dic[i]);
break;
}
}
}
//system("pause");
return 0;
}