求简单,直接用暴力求解方法
对于字典树不够熟悉,题目意思有点奇特
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#define _cdebbug
using namespace std;
int main()
{
//重定向
#ifdef _cdebbug
freopen("F:\\input.txt","r",stdin );
#endif
char dic[1001][21], subStr[21],tmp[21];
int i,j,count,p;
memset(dic,0,sizeof(char)*1001*21);
memset(subStr,0,sizeof(char)*21);
memset(tmp,0,sizeof(char)*21);
//读入字典
count = 0;
while(scanf("%s",dic[count]) != EOF)
count++;
//判断每个字符串从最短开始前缀是否在其他字符串中出现
for (i = 0; i < count; i++)
{
for (j = 1; j <= strlen(dic[i]); j++)
{
strncpy(subStr, dic[i], j);
for (p = 0; p < count; p++)
{
if (p == i)
continue;
strncpy ( tmp , dic[p] , j );
if( strcmp(tmp,subStr) == 0 )
break ;
}
if( p < count )
continue;
if( p == count )
break;
}
printf("%s %s\n",dic[i],subStr);
memset( subStr , 0 , sizeof(char)*21 );
memset( tmp , 0 , sizeof(char)*21 );
}
//解除重定向关闭文件
#ifdef _cdebbug
fclose(stdin);
#endif
return 0 ;
}