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;
}

相关文章
|
6月前
华为机试HJ46:截取字符串
华为机试HJ46:截取字符串
|
7月前
|
机器学习/深度学习
CF1304B Longest Palindrome(可逆转符号,数学分析,回文串)
CF1304B Longest Palindrome(可逆转符号,数学分析,回文串)
23 0
|
7月前
|
算法 Java 程序员
【手绘算法】力扣 3 无重复的最长字符串(Longest Substring Without Repeating Characters)
Hi,大家好,我是Tom。一个美术生转到Java开发的程序员。今天给大家分享的是力扣题第3题,无重复的最长字符串。在解题过程中,也会手写一些伪代码。当然,如果想要完整的源码的话,可以到我的个人主页简介中获取。 这道题呢属于中等难度,评估为四颗星,它的通过率只有39%。
55 0
|
11月前
51nod 1292 字符串中的最大值 V2 (后缀数组)
51nod 1292 字符串中的最大值 V2 (后缀数组)
38 0
LeetCode 395. Longest Substring with At Least K
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 T 的长度。
57 0
LeetCode 395. Longest Substring with At Least K
409. 最长回文串 Longest Palindrome
409. 最长回文串 Longest Palindrome
|
存储 Java 索引
LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 Given a string, find the length of the longest substring without repeating characters. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
971 0