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

 #include <iostream>
#include <string>
#include <algorithm> using namespace std;
string str, t1, t2;
int res = ;
//最普通的遍历
void way1()
{
for (int i = ; i < str.length(); ++i)
{
for (int j = str.length() - ; j > i; --j)
{
t1.assign(str.begin() + i, str.begin() + j + );
t2.assign(t1.rbegin(), t1.rend());
if (t1 == t2)
res = res > t1.length() ? res : t1.length();
}
}
} //利用回文子串中心的两边相同
void way2()
{
for (int i = ; i < str.size(); ++i) {
int j;
for (j = ; i - j >= && i + j < str.size() && str[i + j] == str[i - j]; ++j);//以当前字符为回文中心查找最长回文子串
res= max(res, * j - );//更新回文子串最大长度
for (j = ; i - j >= && i + j + < str.size() && str[i - j] == str[i + + j]; ++j);//以当前字符为回文中心左侧字符查找最长回文子串
res = max(res, * j);//更新回文子串最大长度
}
} //使用动态规划
void way3()
{
int dp[][];
for (int i = ; i < str.length(); i++)
{
dp[i][i] = ;
if (i < str.length() - && str[i] == str[i + ])
{
dp[i][i + ] = ;
res = ;
}
}
for (int L = ; L <= str.length(); L++) {
for (int i = ; i + L - < str.length(); i++) {
int j = i + L - ;
if (str[i] == str[j] && dp[i + ][j - ] == ) {
dp[i][j] = ;
res = L;
}
}
}
} int main()
{
getline(cin, str);
way1();
cout << res << endl;
return ;
}

PAT甲级——A1040 Longest Symmetric String的更多相关文章

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

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  2. PAT 甲级 1040 Longest Symmetric String

    https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...

  3. A1040. Longest Symmetric String

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  4. PAT 1040 Longest Symmetric String[dp][难]

    1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...

  5. 1040. Longest Symmetric String (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...

  6. PAT1040:Longest Symmetric String

    1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

  7. PTA (Advanced Level) 1040 Longest Symmetric String

    1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the lo ...

  8. pat1040. Longest Symmetric String (25)

    1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

  9. 1040 Longest Symmetric String (25分)(dp)

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

随机推荐

  1. 如何清除本机DNS缓存

    如何清除本机DNS缓存 在实际应用过程中可能会遇到DNS解析错误的问题,就是说当我们访问一个域名时无法完成将其 解析到IP地址的工作,而直接输入网站IP却可以正常访问,这就是因为DNS解析出现故障造成 ...

  2. 控制变量行业年份回归时在STATA里怎么操作_stata 分年份回归

    控制变量行业年份回归时在STATA里怎么操作_stata 分年份回归 我希望做一个多元回归,但需要控制年份和行业. (1)年份有7年2006-2012,听说STATA可以自动设置虚拟变量,请问命令是怎 ...

  3. 初识OpenCV-Python - 010: 精致边缘探测

    本节主要介绍使用Canny函数达到边缘探测的结果. Code: import cv2from matplotlib import pyplot as plt img = cv2.imread('bal ...

  4. Activiti业务键(businessKey)

    问题:如何让业务对象和对应的流程 关联? 发现ProcessInstance 有个方法getBusinessKey()可以得到一个businessKey. ProcessInstance 对应数据库中 ...

  5. 服务器访问数据库表mysql

    服务器的MySQL配置就不说了,直接说一些用到的基础命令 登陆 show databases; use 数据库: show tables; 执行sql即可: 一定要有分号 select * from ...

  6. 建立 CRAMFS 包

    下载 cramfs 工具 从 http://prdownloads.sourceforge.net/cramfs/cramfs­1.1.tar.gz下载源代码包. 把下载包拷贝到 dev_home/t ...

  7. Codeforces Round #478 Div2 975A 975B 975C 975D

    A. Aramic script 题目大意:   对于每个单词,定义一种集合,这个集合包含且仅包含单词中出现的字母.给你一堆单词,问有多少种这种集合. 题解:   状压,插入set,取size #in ...

  8. 基于windows消息的响应USB插入或取出

    导语:当有设备进入windows时,系统会向所有的应用层发送WM_DEVICECHANGE消息.进一步根据相应的事件判断设备. LRESULT CALLBACK WndProc(HWND hWnd, ...

  9. Making the Grade

    Making the Grade 给定长度为n的序列\(\{a_i\}\),求构造长度为n的递增序列\(\{b_i\}\),求\(\sum_{i=1}^n|a_i-b_i|\)最小值,\(1 ≤ N ...

  10. dea死锁处理和大事务处理

    死锁处理流程: show full processlist; # 获得当前所有数据库连接 select id, db, user, host, command, time, state, info f ...