【C++】PAT(advanced level)1040. Longest Symmetric String (25)
2014-03-12 10:23
543 查看
1040. Longest Symmetric String (25)
时间限制400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
1.分情况讨论,题目介绍了奇数的情况,还要考虑偶数的情况。
2,如果没有对称,输出1.
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<vector> #include<map> #include<iomanip> using namespace std; int main(){ //freopen("in.txt","r",stdin); char a[1001]; // int m; gets(a); int i=0,sumMax=0,final=1; //cout<<sizeof(a); while(a[i+2]!='\0'){ if(a[i]==a[i+2]){ sumMax=3; int j=1; while(a[i+2+j]!='\0'&&a[i-j]==a[i+2+j]){ sumMax+=2; j++; } } i++; if(sumMax>final){ final=sumMax; } } i=0; while(a[i+1]!='\0'){ if(a[i]==a[i+1]){ sumMax=2; int j=1; while(a[i+1+j]!='\0'&&a[i-j]==a[i+1+j]){ sumMax+=2; j++; } } i++; if(sumMax>final){ final=sumMax; } } cout<<final; // fclose(stdin); system("pause"); return 0; }
相关文章推荐
- 【C++】PAT(advanced level)1047. Student List for Course (25)
- 【C++】PAT(advanced level)1051. Pop Sequence (25)
- 【c++】PAT (Advanced Level) A+B for Polynomials (25)
- 【C++】PAT (advanced level)1074. Reversing Linked List (25)
- 【c++】PAT (Advanced Level)1009. Product of Polynomials (25)
- 【C++】PAT(advanced level)1032. Sharing (25)
- 【C++】PAT(advanced level)1036. Boys vs Girls (25)
- 【C++】PAT (advanced level)1062. Talent and Virtue (25)
- 【C++】PAT(advanced level)1028. List Sorting (25)
- PAT (Advanced Level) 1040. Longest Symmetric String (25) 动态规划
- 【PAT】【Advanced Level】1040. Longest Symmetric String (25)
- 【c++】PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)*
- 【C++】PAT(advanced level)1029. Median (25)
- 【C++】PAT (advanced level)1060. Are They Equal (25)
- 【C++】PAT(advanced level)1055. The World's Richest (25)*
- 【C++】PAT(advanced level)1037. Magic Coupon (25)
- 【C++ 】PAT(advanced level)1063. Set Similarity (25)
- PAT (Advanced Level) 1040. Longest Symmetric String (25)
- 【c++】PAT (Advanced Level)1010. Radix (25)
- 【C++】PAT(advanced level)1048. Find Coins (25)