终曲

Problem Description
最后的挑战终于到了!
站在yifenfei和MM面前的只剩下邪恶的大魔王lemon一人了!战胜他,yifenfei就能顺利救出MM。
Yifenfei和魔王lemon的挑战很简单:由lemon给出三个字符串,然后要yifenfei说出第一串的某个子串,要求该子串长度最小,并且同时包含第2个串和第3个串。
特别地,如果有多个这样的子串,则请输出字母序最小的一个。
 
Input
输入数据首先是一个整数C,表示测试数据有C组;
接着是C组数据,每组包含三行字符串,第一个字符串长度大于1小于100
后面两个串的长度大于1且小于10
 
Output
请对应每组输入数据输出满足条件的最短子串;
如果没有,请输出 No
 
Sample Input
2
abcd
ab
bc
abc
ab
bd
 
Sample Output
abc
No
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main(){
 5     int T;
 6     char s[101];
 7     char temp[101];
 8     char minTemp[101];
 9     char s1[11];
10     char s2[11];
11     int length;
12     int i;
13     int j;
14     int minLength;
15     int tempLength;
16     int flag;
17     
18     scanf("%d",&T);
19     
20     while(T--){
21         scanf("%s%s%s",s,s1,s2);
22         length=strlen(s);
23         minLength=101;
24         flag=0;
25         
26         for(i=1;i<=length;i++){
27             for(j=0;j<length-i+1;j++){
28                 memset(temp,0,101);
29                 strncpy(temp,&s[j],i);
30                 
31                 if(strstr(temp,s1)!=NULL && strstr(temp,s2)!=NULL){
32                     tempLength=strlen(temp);
33                     if(tempLength<minLength){
34                         flag=1;
35                         minLength=tempLength;
36                         strcpy(minTemp,temp);
37                     }
38                     
39                     if(tempLength==minLength){
40                         if(strcmp(temp,minTemp)<0){
41                             flag=1;
42                             strcpy(minTemp,temp);
43                         }
44                     }
45                 }
46                 
47             }
48         }
49         
50         if(flag==1)
51             printf("%s\n",minTemp);
52             
53         else
54             printf("No\n");
55     }
56     return 0;
57 }

 

 

posted @ 2014-11-11 14:23  zqxLonely  阅读(325)  评论(0编辑  收藏  举报