成长轨迹51 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2744:子串】
2012-02-18 11:13
423 查看
题目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代码】
//百炼不能用strrev()要自己写,然后自己写用itoa()也没有……(因此我CE好几次……)
/*为来了遍历全部子串,要按子串长度循环
0 1 2 3
0 1 2
1 2 3
0 1
1 2
2 3
0
1
2
3
*/
【ac代码】
#include <stdio.h> #include <cstring> #include <stdlib.h> int n; char c[120][120]; char cc[120]; char cr[120]; void strrev1(char * source) { char temp[200]; strcpy(temp,source); int len=strlen(source); for(int i=0;i<len;i++) { source[i]=temp[len-1-i]; } } int foundsubstr(char *source) { int mlen=strlen(source); int sublen=mlen; bool found=true; while(sublen>0) { for(int j=0;j<=mlen-sublen;j++) { strncpy(cc,source+j,sublen); //【生成长度为sublen,从原串c[mindex]第j位开始的子串】 strncpy(cr,source+j,sublen); cc[sublen]=cr[sublen]='\0'; strrev1(cr); found=true; for(int k=0;k<n;k++) { if(strstr(c[k],cc)==NULL && strstr(c[k],cr)==NULL) { found = false; break; } } if(found) { return sublen; } } sublen--; } return 0;//【如果没有找到】 } int main() { int t; scanf("%d",&t); for(int i=0;i<t;i++) { scanf("%d",&n); int mlen=120; int mindex=0; for(int j=0;j<n;j++) { scanf("%s",&c[j]); int temp=strlen(c[j]); if(temp<mlen) { mindex=j; mlen=temp; } } int len = foundsubstr(c[mindex]); printf("%d\n",len); } return 0; }
相关文章推荐
- 成长轨迹47 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2820:古代密码】
- 成长轨迹48 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2818:密码】
- 成长轨迹49 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2819:W的密码】
- 成长轨迹44 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2799、2976、2975、2742】
- 成长轨迹45 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2797:最短前缀】
- 成长轨迹46 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2804:词典】
- 成长轨迹50 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2974:487-3279】
- 成长轨迹52 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2743:字符串判等】
- 成长轨迹53 【ACM算法之路 百炼poj.grids.cn】【简单枚举】【2747、2810、2692、2977】
- 成长轨迹54 【ACM算法之路 百炼poj.grids.cn】【简单枚举】【2739:计算对数】
- 成长轨迹55 【ACM算法之路 百炼poj.grids.cn】【简单枚举】【2812:恼人的青蛙】
- 成长轨迹57 【ACM算法之路 百炼poj.grids.cn】【递归】【2754、2816、2753、】
- 成长轨迹58 【ACM算法之路 百炼poj.grids.cn】【递归】【1664:放苹果】
- 成长轨迹56 【ACM算法之路 百炼poj.grids.cn】【简单枚举】【2811:熄灯问题】
- 成长轨迹59 【ACM算法之路 百炼poj.grids.cn】【递归】【2694:逆波兰表达式】
- 成长轨迹60 【ACM算法之路 百炼poj.grids.cn】【递归】【2756:二叉树】
- 成长轨迹61 【ACM算法之路 百炼poj.grids.cn】【动态规划】【2806、1661、2757】
- OpenJudge百炼-2744-子串-C语言-字符串处理
- 成长轨迹42 【ACM算法之路 百炼poj.grids.cn】【数值转换】【2972、2798、2735、2734、2973】
- 成长轨迹43 【ACM算法之路 百炼poj.grids.cn】【数值转换】【2765:八进制小数】