Hat’s Words

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct snode{
   char str[20];
   int len;
     bool operator <(const snode& other) const
   {
       if(len<=other.len)
  return true;
  else
return false;
   }
   
}Snode;




Snode mys[50000];
typedef struct tnode{
   char name[20];


}Tnode;


bool mycompare(const Tnode& a,const Tnode& b)
{
    if(strcmp(a.name,b.name)==-1)
{
 return true;
}
else
return false;


}
Tnode mynode[50000];
typedef struct node{


node(){
isword=false;
for(int i=0;i<26;i++)
{
 child[i]=NULL;
}

}
bool isword;
node* child[26]; 
}Node;
Node* root=NULL;
bool search( char* str1,int a,int b)
{

    Node* temp=root;
   
for(int i=a;i<=b;i++)
{
   if(temp->child[str1[i]-'a']!=NULL)
{

temp=temp->child[str1[i]-'a'];
   
}
else{
    return false;
}
        

}
if(temp->isword)
return true;
else
return false;
    
   
}


void insert(const char* str2)
{


if(root==NULL)
root=new Node();
     int len=strlen(str2);
Node* temp=root;
for(int i=0;i<len;i++)
{
    if(temp->child[str2[i]-'a']==NULL)
{
    temp->child[str2[i]-'a']=new Node();


 
}
else{
     
 
}
temp=temp->child[str2[i]-'a'];
 
}
temp->isword=true;






}




int main()
{
//freopen("in.txt","r",stdin);
   char str[20];
   int i=0;
   while(scanf("%s",str)!=EOF)
   {
       strcpy(mys[i].str,str);
  mys[i++].len=strlen(str);
 
  
   
   }
   sort(mys,mys+i);
  
   int t=0;
   for(int j=0;j<i;j++)
   {
       insert(mys[j].str);
  if(j>=2)
  {
     for(int k=0;k<strlen(mys[j].str)-1;k++)
 {
    if(search(mys[j].str,0,k)&&search(mys[j].str,k+1,strlen(mys[j].str)-1))
{
    strcpy(mynode[t++].name,mys[j].str);

break;
 
}
 }
  
  }
   
   }
 
   sort(mynode,mynode+t,mycompare);
   for(int m=0;m<t;m++)
   {
   
     printf(mynode[m].name);
printf("\n");
   }


  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值