3801. String LD

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:

  1. A word becomes empty string, or

  2. 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 }

 

转载于:https://www.cnblogs.com/yushuo1990/p/4309714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值