成长轨迹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/p/2356845.html