题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=585
找一个字符串是否是另一个的前缀 ,我用了一个字符串函数,然后比较了下就行了
View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 int main() 5 { 6 int i,j,g,k,n,m,f = 0,q = 0,flag; 7 char c[11],d[14][12],*ptr; 8 while(gets(c)!=NULL) 9 { 10 if(c[0] == '9') 11 { 12 q++; 13 flag = 0; 14 for(i = 0 ; i < f ; i++) 15 { 16 for(j = 0 ; j < f ; j++) 17 { 18 if(i!=j) 19 { 20 if(strcmp(d[i],d[j]) == 0) 21 { 22 flag = 1; 23 break; 24 } 25 else 26 { 27 if(strlen(d[i])>strlen(d[j])) 28 { 29 ptr = strstr(d[i], d[j]); 30 if(ptr) 31 { 32 if(strcmp(ptr,d[i]) ==0) 33 { 34 flag = 1; 35 break; 36 } 37 } 38 } 39 else 40 { 41 ptr = strstr(d[j],d[i]); 42 if(ptr) 43 { 44 if(strcmp(ptr,d[j])==0) 45 { 46 flag = 1; 47 break; 48 } 49 } 50 } 51 } 52 } 53 } 54 if(flag == 1) 55 break; 56 } 57 if(flag == 0) 58 printf("Set %d is immediately decodable\n",q); 59 else 60 printf("Set %d is not immediately decodable\n",q) ; 61 f = 0; 62 } 63 else 64 strcpy(d[f++],c); 65 } 66 return 0; 67 }