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. System.DateTime.cs

    ylbtech-System.DateTime.cs 1. 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 ...

  2. sql还原数据库时候改变数据库名

    需求:在做图书馆数据整合时候,由于有两个校区,用的是分离开的同一个数据库,数据库名字都一样的,现在我要整合在一起,我的想法是把两个数据库先还原到我本地,用写好的脚本整合到一起.所以,我还原两个数据库时 ...

  3. (转)Windows中杀死占用某个端口的进程

    启动tomcat时候,控制台报错,发现是端口占用,于是寻找方法关闭对应的程序. 从网上找了好久,尝试之后,发现不行.开始自己尝试,终于,成功的将占用端口的进程杀掉.在此记录下过程(以8081端口为例) ...

  4. Pthread spinlock自旋锁

    锁机制(lock) 是多线程编程中最常用的同步机制,用来对多线程间共享的临界区(Critical Section) 进行保护. Pthreads提供了多种锁机制,常见的有:1) Mutex(互斥量): ...

  5. 【学术篇】NOIP2016 D1T3 luogu1850换教室

    题目链接:点击这里献出你宝贵的时间(是用来做题不是捐赠Emmmm).. Emmmm我太弱了= = 做完这题我觉得我应该去打星际..这题怎么就有重边了呢.. 这题就是一道期望= =当时考场上好像完全不会 ...

  6. js怎样把URL链接的参数截取出来

    有时候,A页面参数需要传递到B页面,则把参数拼接到跳转B页面的url上,这时怎样在另一个页面截取A页面传递的参数呢,主要代码如下 /** * 获取指定的URL参数值 URL:http://www.qu ...

  7. CSS所有选择器

    .class .intro 选择所有class="intro"的元素 1 #id #firstname 选择所有id="firstname"的元素 1 * * ...

  8. webpack打包指定HTML的文件并引入指定的chunks

    1. 安装 html-webpack-plugin npm install html-webpack-plugin --save-dev 2. 在webpack.config.js中配置 const ...

  9. CobaltStrike与Metasploit实战联动

    前言 CobalStrike 与 Metasploit 均是渗透利器,各有所长.前者更适合做稳控平台,后者则更擅长内网各类探测搜集与漏洞利用.两者更需要灵活的联动,各自相互依托,从而提升渗透的效率. ...

  10. JeePlus-Note:笔记1

    ylbtech-JeePlus-Note:笔记1 1.返回顶部 1. 1.JeePlus/代码生成器http://localhost:8081/a/login 2.manager/Java基础框架ht ...