成长轨迹51 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2744:子串】

题目http://poj.grids.cn/practice/2744

//百炼不能用strrev()要自己写,然后自己写用itoa()也没有……(因此我CE好几次……)
/*为来了遍历全部子串,要按子串长度循环
  0 1 2 3
  0 1 2 
     1 2 3
  0 1
     1 2
        2 3
  0
     1
        2
           3
*/

【ac代码】

 1 #include <stdio.h>
2 #include <cstring>
3 #include <stdlib.h>
4
5 int n;
6 char c[120][120];
7 char cc[120];
8 char cr[120];
9
10 void strrev1(char * source)
11 {
12 char temp[200];
13 strcpy(temp,source);
14 int len=strlen(source);
15 for(int i=0;i<len;i++)
16 {
17 source[i]=temp[len-1-i];
18 }
19 }
20
21 int foundsubstr(char *source)
22 {
23 int mlen=strlen(source);
24 int sublen=mlen;
25 bool found=true;
26 while(sublen>0)
27 {
28 for(int j=0;j<=mlen-sublen;j++)
29 {
30 strncpy(cc,source+j,sublen);
31 //【生成长度为sublen,从原串c[mindex]第j位开始的子串】
32 strncpy(cr,source+j,sublen);
33 cc[sublen]=cr[sublen]='\0';
34 strrev1(cr);
35
36 found=true;
37 for(int k=0;k<n;k++)
38 {
39 if(strstr(c[k],cc)==NULL && strstr(c[k],cr)==NULL)
40 {
41 found = false;
42 break;
43 }
44 }
45 if(found)
46 {
47 return sublen;
48 }
49 }
50 sublen--;
51 }
52 return 0;//【如果没有找到】
53 }
54
55
56
57 int main()
58 {
59 int t;
60 scanf("%d",&t);
61 for(int i=0;i<t;i++)
62 {
63
64 scanf("%d",&n);
65 int mlen=120;
66 int mindex=0;
67 for(int j=0;j<n;j++)
68 {
69 scanf("%s",&c[j]);
70 int temp=strlen(c[j]);
71 if(temp<mlen)
72 {
73 mindex=j;
74 mlen=temp;
75 }
76 }
77 int len = foundsubstr(c[mindex]);
78 printf("%d\n",len);
79 }
80 return 0;
81 }

 

转载于:https://www.cnblogs.com/zeedmood/archive/2012/02/18/2356845.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值