NYOJ308-Substring,
分享于 点击 1814 次 点评:228
NYOJ308-Substring,
Substring时间限制:1000 ms | 内存限制:65535 KB
难度:1
描述
You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.
Note well: The substring and its reversal may overlap partially or completely. The entire original string is itself a valid substring . The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.
输入
The first line of input gives a single integer, 1 ≤ N ≤ 10, the number of test cases. Then follow, for each test case, a line containing between 1 and 50 characters, inclusive. Each character of input will be an uppercase letter ('A'-'Z').
输出
Output for each test case the longest substring of input such that the reversal of the substring is also a substring of input
样例输入
3
ABCABA
XYZ
XCVCX
样例输出
ABA
X
XCVCX
来源
第四届河南省程序设计大赛
题意是找最长子串反过来还是其子串,不是找最长的回文串
AC代码:
#include<stdio.h>
#include<string.h>
char a[110],b[110],c[110];
int Find(char a[],char c[],int n,int k)//判断c字串反过来还是不是a的字串
{
int j=k-1,i=0,x=0,flag=0;
while(1)
{
if(j==-1)
{
flag=1;
break;
}
if(x>n-1)
break;
if(c[j]==a[i])
{
j--;i++;
}
else
{
j=k-1;i=x++;
}
}
return flag;
}
int main()
{
int i,j,n,m,len;
scanf("%d",&m);
while(m--)
{
scanf("%s",a);
n=strlen(a);
int flag=0;len=0;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(j<=i)
{
memset(c,0,sizeof(c));
int k=0;
/*for(int p=j;p<=i;p++)
printf("%c",a[p]);
puts("");*/
for(int p=j;p<=i;p++)//找出子串
{
c[k++]=a[p];
}
if(Find(a,c,n,k)==1)//判断子串的反串是不是仍是子串
{
if(k>len)//找到新的符合条件的并且长度比原来大的更新
{
len=0;
for(int v=0;v<k;v++)
{
b[len++]=c[v];
}
}
}
}
}
}
for(i=0;i<len;i++)//输出最终答案
printf("%c",b[i]);
puts("");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}
相关文章
- 暂无相关文章
用户点评