PAT 1040 Longest Symmetric String[dp][难]
1040 Longest Symmetric String (25)(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
题目大意:
//自己写的土鳖方法,以每个字符串为对称中心进行判断。一开始只考虑了对称长度是偶数的情况,没有考虑奇数。得了21分。加上判断奇数的,只得了23分,还是有一个测试点没过去,没找到是什么原因,暂时放一下。
#include <iostream>
#include <algorithm>
#include <string>
#include<stdio.h>
using namespace std; int main() {
string s="";
char ch;
while(ch=getchar()){
if(ch=='\n')break;
if(ch==' ')s+=" ";
else s+=ch;
}
int len=s.size();
int ct=,tp=;
for(int i=;i<len-;i++){
for(int j=;j<=i;j++){
if(i+j>=len)break;
if(s[i-j]==s[i+j])
tp++;
else break;
}
if(*tp+>ct)ct=*tp+;
tp=;
}
tp=;
for(int i=;i<len-;i++){
for(int j=;j<=i;j++){
if(i+j+>=len)break;
if(s[i-j]==s[i+j+])
tp++;
else break;
}
if(*tp>ct)ct=*tp;
tp=;
}
cout<<ct;
return ;
}
这个代码也是通过判断对阵中心,不过使用了reverse函数, 以前见过的,对string判断对称,使用reverse
通过截取,如果是偶数时,那么对称中心就是中间靠左的那个,(和中位数一样。)
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s;
getline(cin, s);
int len = ;
for (int i = ; i < s.size(); i++){
for (int j = ; j <= min(i, int(s.size())--i); j++){
//这个最小值表示向左向右还最多可以截取几个。是需要min来限制的。
string s2 = s.substr(i-j,*j+);
string s3 = s2;
reverse(s2.begin(), s2.end());
if (s2 == s3){
if (s2.size() > len)
len = s2.size();
}
string s4 = s.substr(i-j, *j+);//这个如果i指向,最后一个,是会和上边重判的。是截取一个。
string s5 = s4;
reverse(s4.begin(), s4.end());
if (s4 == s5){
if (s4.size() > len)
len = s4.size();
}
}
}
printf("%d\n", len);
return ;
}
//下面是大佬的dp版本:https://www.liuchuo.net/archives/2104
#include <iostream>
#include<stdio.h>
using namespace std;
int dp[][];//dp[i][j]只有0和1取值,表示i和j之间是否是
int main() {
string s;
getline(cin, s);//直接getline可以读进去字符串里。
int len = s.length(), ans = ;
for(int i = ; i < len; i++) {
dp[i][i] = ;
if(i < len - && s[i] == s[i+]) {
dp[i][i+] = ;
ans = ;
}
}
for(int L = ; L <= len; L++) {
for(int i = ; i + L - < len; i++) {//总长度的限制。
int j = i + L -;
if(s[i] == s[j] && dp[i+][j-] == ) {
dp[i][j] = ;
ans = L;
}
}
}
printf("%d", ans);
return ;
}
//dp数据只有0和1取值,dp[i][j]表示i到j是否是对称的,为了保证状态的转移,使用长度作为循环,因为2很好判断,那么就从L=3开始,i每次都从0开始,那么j就是那个对应的结束,要满足的条件自然是j+L-1<len了。而最终的答案自然是最大的L。还有dp[i+1][j-1]也是神了,这就是坐进右退判断对称的。
PAT 1040 Longest Symmetric String[dp][难]的更多相关文章
- PAT 1040 Longest Symmetric String
#include <cstdio> #include <cstdlib> using namespace std; ]; ]; int syslen(char str[], i ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- 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 ...
- 1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...
- 1040 Longest Symmetric String (25分)(dp)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- PAT 甲级 1040 Longest Symmetric String
https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...
- 1040 Longest Symmetric String
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT (Advanced Level) 1040. Longest Symmetric String (25)
暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ...
随机推荐
- spring基础---->spring自定义初始化(一)
这里我们简单的实现一下spring中的初始化bean,以大概了解他的流程.受委屈几乎是一个人成长最快的途径,吃下去的是委屈,消化掉后得到的是格局. spring的自定义初始化 测试的项目结构如下: 一 ...
- 原生js(二)
js的同步.异步和延迟 1.默认情况下,js是同步和阻塞DOM解析的.在解析DOM的过程中,当遇到script时,会暂停DOM解析,开始请求script并执行js,执行完成之后再接着解析DOM树. 2 ...
- MySQL DROP 大表时的注意事项
对于表的删除,因为InnoDB引擎会在table cache层面维护一个全局独占锁一直到DROP TABLE完成为止,这样,对于表的其他操作会被HANG住.对于较大的表来说,DROP TABLE操作可 ...
- LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)
题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description Problem: 已知两个有序链表(链表中的数 ...
- Linux "bring up eth0 failed, eth0 seems not be presernt" 问题解决方案
=========1.问题========== 重启网卡的时候出现"bring up eth0 failed, eth0 seems not be presernt", 提示找不到 ...
- Coding和Git的环境搭建
Github太慢了.打开网页慢,下载也只有几kb. 于是找了国内的Git,据说coding不错.就申请了个. 其实csdn也有...但是没人家的专业... 1 注册coding https://co ...
- 【BZOJ1502】[NOI2005]月下柠檬树 Simpson积分
[BZOJ1502][NOI2005]月下柠檬树 Description 李哲非常非常喜欢柠檬树,特别是在静静的夜晚,当天空中有一弯明月温柔地照亮地面上的景物时,他必会悠闲地坐在他亲手植下的那棵柠檬树 ...
- jenkins之另辟蹊径实现根据svn项目实现智能选择
项目要求,根据svn选择的trunk或branches及tags里的各分支,动态选择参数.一开始认为很简单,直接用jenkins中的List Subversion tags插件及active choi ...
- Unity3D 面试ABC
最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...
- hiredis安装及测试
(1) redis环境搭建 (2) hiredis下载地址及C API github (3) hiredis安装 我是把libhiredis.so放到/usr/local/lib/中, ...