第四届河南省程序设计大赛 Substring nyoj 308

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,%>_<%,又把题目读了好几遍(其实根本没读懂),还是不知道哪儿错了,(样例坑爹,老是误导人),后来无奈百度了下,看别人博客把题意看懂了,原来是求正序与倒序的最长公共子序列,如果样例是这样子(453254),结果应该是45,就不是4了,自己想了想,四个for循环,AC~


01. #include<stdio.h>
02. #include<string.h>
03. int main()
04. {
05. int N;
06. scanf("%d",&N);
07. while(N--)
08. {
09. char a[100],b[100];
10. scanf("%s",a);
11. int i,j,k,v,max=0,qi,zhong,n=strlen(a);
12. for(i=0; i<n; i++)
13. b[n-i-1]=a[i];
14. for(i=0; i<n-1; i++)
15. for(j=i+1; j<n; j++)
16. {
17. for(k=0; k<n-(j-i); k++)
18. {
19. int f=0,d=i;
20. for(v=0; v<j-i+1; v++)
21. if(a[d+v]!=b[k+v])
22. {
23. f=1;
24. break;
25. }
26. if(!f&&j-i+1>max)
27. qi=i,zhong=j,max=j-i+1;
28. }
29. }
30. if(max)
31. for(i=qi; i<=zhong; i++)
32. printf("%c",a[i]);
33. else
34. printf("%c",a[0]);
35. printf("\n");
36. }
37. }


你可能感兴趣的:(省赛)