NYOJ308(字符串)

解题思路:刚开始以为是找最长的回文子串,陷入了误区。这道题类似回文子串,但不同的是回文子串的意思是正着看和倒着看是相同的。而本题的关键在于" the reversal of the substring is also a substring of input ”,就是它的逆串也存在在这个字符串中。比如ABCDBA的最长回文子串是A,但按本题结果应该是AB。

注意:应该输出第一个出现这种满足题意的子串,而且必须是连续的。(求最长公共子序列不要求连续,注意区分。)

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int map[55][55];
 6 int main()
 7 {
 8     int n,i,j,len,m;
 9     char str1[55],str2[55];
10     cin>>n;
11     while(n--)
12     {
13         cin>>str1;
14         len=strlen(str1);
15         memset(map,0,sizeof(map));
16         for(i=0;i<len;i++)
17             str2[i]=str1[len-1-i];
18         int maxlen=0;
19         for(i=0;i<len;i++)
20             for(j=0;j<len;j++)
21                 if(str1[i]==str2[j]) 
22                 {
23                     map[i+1][j+1]=map[i][j]+1;
24                     if(maxlen<map[i+1][j+1]) 
25                     {
26                         maxlen=map[i+1][j+1];
27                         m=i+1;
28                     }
29                 }
30         for(i=m-maxlen;i<m;i++)
31             cout<<str1[i];
32         cout<<endl;
33     }
34     return 0;
35 }

 

posted @ 2012-03-09 10:08  笑巧  阅读(199)  评论(0编辑  收藏  举报