您的位置:首页 > 其它

成长轨迹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代码】

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐