浙大pat1040 Longest Symmetric String(25 分)

1040 Longest Symmetric String(25 分)

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

题目思路:这个题实质是求最长回文子串的长度。有两种思路,第一种是以中间元素为中心,向两边扩散,这种写法及其好写,但是麻烦的是遇到abba这种偶数个回文串的情况就不知道怎么办了。在网上找到了一个神奇的方法,就是往每个字符串两边添加一个字符,之后再将总数除以2即可这是该思路的链接https://blog.csdn.net/sunbaigui/article/details/8656933贴一下我写的代码:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
	string so;
	getline(cin,so);
	int length = so.length();
	string s;
	for(int i=0;i<length;i++)
	{
		s.push_back(‘-‘);
		s.push_back(so[i]);
	}
	s.push_back(‘-‘);

	int max = -1;
	for(int i=1;i<s.length()-1;i++)
	{
		int tmp_length=1;
		int p,q;
		p = i-1;
		q = i+1;
		while (s[p] == s[q])
		{
			p--;
			q++;
			tmp_length+=2;
			if(p<0||q>s.length())
			break;
		}
		if(max<tmp_length)
			max = tmp_length;
	}
	cout<<max/2;
}

  第二种思路就是把串翻转过来,转化成求两个串最大子序列的问题(一般都有板子的)这里借鉴了(https://blog.csdn.net/zhangpiu/article/details/50733603)

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

string maxSubString(string s1, string s2){
	int xlen = (int)s1.size(), ylen = (int)s2.size();
	vector<vector<int>> m(xlen, vector<int>(ylen, 0));
	int maxlen = -1, index = -1;

	for(int i = 0; i < xlen; ++i){
		for(int j = 0; j < ylen; ++j){
			if(s1[i] == s2[j]){
				if(i == 0 || j == 0) m[i][j] = 1;
				else m[i][j] = m[i-1][j-1] + 1;

				if(maxlen < m[i][j]){
					maxlen = m[i][j];
					index = i - maxlen + 1;
				}
			}
		}
	}

	return s1.substr(index, maxlen);
}

int main(){
	string s;
	getline(cin, s);

	string rs(s);
	reverse(begin(rs), end(rs));

	cout << maxSubString(s, rs).size();

	return 0;

  

原文地址:https://www.cnblogs.com/SK1997/p/9588868.html

时间: 2024-12-12 18:09:28

浙大pat1040 Longest Symmetric String(25 分)的相关文章

PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) 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. In

pat1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 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?", th

PAT 1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) 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

PAT1040. Longest Symmetric String

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 Specifica

1040. Longest Symmetric String (25)

题目例如以下: 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 S

PAT (Advanced Level) 1040. Longest Symmetric String (25)

暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right&

PAT甲级——A1040 Longest Symmetric String

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 co

PAT 1040 Longest Symmetric String

#include <cstdio> #include <cstdlib> using namespace std; char line[1001]; char line2[2003]; int syslen(char str[], int start) { int len = 1; int p = start - 1; int q = start + 1; while (p >=0 && str[q] != '\0' && str[p] ==

浙大pat1039 Course List for Student(25 分)

1039 Course List for Student(25 分) Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query. In