nyoj 308 Substring,nyoj308substring
分享于 点击 11081 次 点评:23
nyoj 308 Substring,nyoj308substring
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
-
刚开始做的时候,英语没有理解清楚,做成求最长回文串了,wa了一次
又读了一下题目意思,以后还是认真仔细点吧
题目意思:
在给出的字符串中找到一个最长子串,条件是其子串的逆串也出现在字符串中
因为字符串最长为50,直接暴力
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s1[55];
char s2[55];
char s3[55];
char s4[55];
int main(){
int i,j,k,t,n,Max,len,l;
scanf("%d",&t);
while(t--){
Max=-1;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
scanf("%s",s1);
len=strlen(s1);
for(i=0,j=len-1;i<len;i++,j--)
s2[j]=s1[i];
if(!strcmp(s1,s2)){
printf("%s\n",s1);
continue;
}
Max=-1;
for(i=0;i<len;i++){
for(j=1;j<=len;j++){
l=0;
memset(s3,0,sizeof(s3));
for(k=i;k<j;k++){
s3[l++]=s1[k];
}
if(strstr(s2,s3)&&l>Max){
Max=l;
strcpy(s4,s3);
}
}
}
printf("%s\n",s4);
}
return 0;
}
相关文章
- 暂无相关文章
用户点评