一天,Meizi送了Lee一个礼物,Lee很高兴,可是Meizi不让他当面拆开包装。经过一段安定祥和的时光后,Lee回到了宿舍。他迫不急待地想打开礼物,拆开包装后却发现盒子需要密码才能打开……Lee郁闷了,密码是什么呢?经过一番搜索,他发现盒子底下写有Meizi的留言:
亲爱的,你从一个单词里选出一些字符按照原来的顺序组成新的单词,如果其它单词也都能用这个方法组出一样的新单词,那么所有这样的单词里,最长的那个的长度就是密码哦。希望你喜欢这个礼物啦 ^_^
在留言下面有n个单词,可是Lee苦恼地发现,Meizi给的单词有些很长,所以没法人肉出来……但是他真的很想知道Meizi的礼物是什么,只好向你求助啦。请你帮助Lee打开盒子吧。
Input
第一行包含一个正整数n(2<=n<=10),接下来n行每行一个单词。每个单词最多有100个字母,且所有单词的长度的乘积不会超过106。
Output
输出一行,包含一个正整数,为打开盒子的密码。
Sample Input
3
i
love
you
Sample Output
0
Hint
附加样例:
Input:
2
axbyb
GakbaEbdc
Output:
3
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 0
#define ERROR 1
#define MAX 100
#define status int
typedef struct word
{
char *buf;
int flag;
}word,words[10];
status creat_dict(words &dict);//创建单词表
status search(word w,char ch);//从flag处开始查找字符ch
int get_min_word(words dict);//在num个单词中找寻最短的单词,并返回在dict中的索引
status set_flag(words &dict,char ch);//设置新flag
int num;//全局变量,单词数目
int main()
{
int i,j;
int length;
words dict;
char *min_word;
scanf("%d",&num);//输入单词个数
creat_dict(dict);//创建长度为n的单词表
min_word=dict[get_min_word(dict)].buf;//长度最短单词
length=0;
for(i=0;i<strlen(min_word);i++)
{
for(j=0;j<num;j++)
if(search(dict[j],min_word[i])==ERROR)
break;
if(j==num)//没有break,查询成功
{
set_flag(dict,min_word[i]);
length++;
}
}
printf("%d\n",length);
//从最短的单词开始依次查找
system("pause");
return 0;
}
status creat_dict(words &dict)
{
int i;
for(i=0;i<num;i++)//初始化单词表
{
dict[i].flag=0;
dict[i].buf=(char *)malloc(MAX*sizeof(char));
}
for(i=0;i<num;i++)//依次输入单词
{
scanf("%s",dict[i].buf);
dict[i].buf[strlen(dict[i].buf)]='\0';
getchar();
}
return OK;
}
int get_min_word(words dict)
{
int i,index;
int length;
index=0;
length=101;
for(i=0;i<num;i++)
if(length<strlen(dict[i].buf))
{
length=strlen(dict[i].buf);
index=i;
}
return index;
}
status search(word w,char ch)
{
int i;
for(i=w.flag;i<strlen(w.buf);i++)
if(w.buf[i]==ch)
return OK;
return ERROR;
}
status set_flag(words &dict,char ch)
{
int i,j;
for(i=0;i<num;i++)//遍历所有单词
for(j=dict[i].flag;j<strlen(dict[i].buf);j++)//从当前flag处,遍历,找到ch并设置新flag
if(dict[i].buf[j]==ch)
{
dict[i].flag=j;
break;
}
return OK;
}