标签:
Description
Stringld (left delete) is a function that gets a string and deletes its leftmost character (for instance Stringld("acm") returns "cm").
You are given a list of distinct words, and at each step, we apply stringld on every word in the list. Write a program that determines the number of steps that can be applied until at least one of the conditions become true:
A word becomes empty string, or
a duplicate word is generated.
For example, having the list of words aab, abac, and caac, applying the function on the input for the first time results in ab, bac, and aac. For the second time, we get b, ac, and ac. Since in the second step, we have two ac strings, the condition 2 is true, and the output of your program should be 1. Note that we do not count the last step that has resulted in duplicate string. More examples are found in the sample input and output section.
Input
There are multiple test cases in the input. The first line of each test case is n (1 ≤ n ≤ 100), the number of words.
Each of the next n lines contains a string of at most 100 lower case characters.
The input terminates with a line containing 0.
Output
For each test case, write a single line containing the maximum number of stringld we can call.
Sample Input
4
aaba
aaca
baabcd
dcba
3
aaa
bbbb
ccccc
0
Sample Output
1
2
注意点:
1、字符串str和计数器num_count一定记得每次之后清除数据。
for(i=0;i<n;i++)//清空字符串数组
memset(str[i],0,sizeof(str[i]));
num_count = -1;
2、大数组要开在main函数之外。
代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 char str[100][100]; 5 6 void stringld(int x); 7 int is_nullorsame(int x); 8 9 int main(void) 10 { 11 int n=0,i=0; 12 int num_count=-1; 13 while(scanf("%d",&n) != EOF){ 14 if(n == 0) break; 15 for(i=0;i<n;i++) 16 scanf("%s",str[i]); 17 //处理 18 while(is_nullorsame(n) == 0){ 19 num_count++; 20 stringld(n); 21 } 22 printf("%d\n",num_count); 23 for(i=0;i<n;i++)//清空字符串数组 24 memset(str[i],0,sizeof(str[i])); 25 num_count = -1; 26 } 27 28 //system("PAUSE"); 29 return 0; 30 } 31 32 void stringld(int x) 33 { 34 int i,j; 35 for(i=0;i<x;i++){ 36 for(j=0;j<strlen(str[i]);j++) 37 str[i][j] = str[i][j+1]; 38 } 39 } 40 41 int is_nullorsame(int x) 42 { 43 int i,j; 44 //先判是否为空 45 for(i=0;i<x;i++) 46 if(strlen(str[i]) == 0) return 1; 47 //判断相同 48 for(i=0;i<x;i++) 49 for(j=i+1;j<x;j++) 50 if(strcmp(str[i],str[j]) == 0) return 1; 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/yushuo1990/p/4309714.html