首页 > 其他 > 详细

nyoj_308_Substring_201405091611

时间:2014-05-10 06:20:36      阅读:365      评论:0      收藏:0      [点我收藏+]

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
来源
第四届河南省程序设计大赛
上传者
张云聪
bubuko.com,布布扣
 1 #include <stdio.h>
 2 #include <string.h>
 3 int map[60][60];
 4 int main()
 5 {
 6     int T;
 7     scanf("%d",&T);
 8     while(T--)
 9     {
10         int i,j,len,max=0,t;
11         char str1[60],str2[60];
12         memset(map,0,sizeof(map));
13         scanf("%s",str1);
14         len = strlen(str1);
15         for(i=0;i<len;i++)
16         str2[i]=str1[len-1-i];
17         for(i=0;i<len;i++)
18         {
19             for(j=0;j<len;j++)
20             {
21                 if(str1[i]==str2[j])
22                 map[i+1][j+1] = map[i][j]+1;
23                 if(map[i+1][j+1]>max)
24                 {
25                     max = map[i+1][j+1];
26                     t = i+1;
27                 }
28             }
29         }
30         for(i=t-max;i<t;i++)
31         printf("%c",str1[i]);
32         printf("\n");
33     }
34     return 0;
35 }
bubuko.com,布布扣

//最长公共子串

//http://i.cnblogs.com/EditPosts.aspx?postid=3582994

nyoj_308_Substring_201405091611,布布扣,bubuko.com

nyoj_308_Substring_201405091611

原文:http://www.cnblogs.com/xl1027515989/p/3719326.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!