Find Palindrome(dp 金马五校赛-东华大学)
来源:互联网 发布:seo关键字是什么 编辑:程序博客网 时间:2024/05/02 20:56
Problem E : Find Palindrome
From: DHUOJ, 2017060305
Description
Given a string S, which consists of lowercase characters, you need to find the longest palindromic sub-string.
A sub-string of a string S is another string S' that occurs "in"S. For example, "abst" is a sub-string of "abaabsta". A palindrome is a sequence of characters which reads the same backward as forward.
Input
There are several test cases.
Each test case consists of one line with a single string S (1 ≤ |S | ≤ 50).
Output
For each test case, output the length of the longest palindromic sub-string.
Sample Input
sasadasabxabxzhuyuan
Sample Output
713
Author: Kenny
利用dp来记录当前位置的最长回文串长度
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std; #define MIN(a,b) ((a) < (b) ? (a) : (b)) int maxid; // 最长回文子串下标int LPS_rb[100]; // i为中心的回文子串右边界下标right borderchar str[100]; // 原字符串处理后的副本int maxlen;void LPS_linear(char * X, int xlen){ maxlen = maxid = 0; str[0] = '$'; // 将原串处理成所需的形式 char *p = str; *(++p)++ = '#'; while((*p++ = *X++) != '\0') { *p++ = '#'; } for(int i = 1; str[i]; ++i) // 计算LPS_rb的值 { if(maxlen > i) // 初始化LPS[i] { LPS_rb[i] = MIN(LPS_rb[2*maxid-i],(maxlen-i)); }else { LPS_rb[i] = 1; } while(str[i-LPS_rb[i]] == str[i+LPS_rb[i]]) // 扩展 { ++LPS_rb[i]; } if(LPS_rb[i]-1 > maxlen) { maxlen = LPS_rb[i]-1; maxid = i; } }} int main(){ char s[51]; while(cin>>s){ LPS_linear(s,strlen(s)); printf("%d\n", maxlen); } return 0;}
阅读全文
0 0
- Find Palindrome(dp 金马五校赛-东华大学)
- Sailing(BFS 金马五校赛-东华大学 )
- STEED Cards(打表 金马五校赛-东华大学)
- Count the Number(set 金马五校赛-东华大学)
- An Easy Problem(水 金马五校赛-东华大学)
- Frog's Jumping(欧拉函数 金马五校赛-东华大学)
- 神无月排位赛(模拟 金马五校赛-上海大学)
- 添加好友(快速幂 金马五校赛-上海大学)
- 2017-金马五校程序设计竞赛-E -Find Palindrome
- 2017上海金马五校程序设计竞赛 E:Find Palindrome
- 2017年上海金马五校程序设计竞赛:Find Palindrome
- 东华大学之游
- 字符串进制转换(进制转换 金马五校赛-上海大学)
- A序列(最长上升子序列 金马五校赛-上海大学)
- I Love ces(大小写字母转换 金马五校赛-上海大学)
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome
- 第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛(部分题解)
- XMind保存文件出错解决办法
- redis windows安装并使用
- linux服务器快速配置mysql
- 【android】点击事件穿透解决方案
- CNN初探(五)------Rich feature hierarchies for accurate object detection and semantic segmentation
- Find Palindrome(dp 金马五校赛-东华大学)
- 调用DLL函数,出现错误 Run-Time Check Failure #0
- Git下载及安装环境配置
- 多重背包问题
- AI黑科技来袭 2017北京硅谷高创会携PanguVR一窥智慧未来
- 谈谈java的运行机制
- Red packet NBUT
- 关于6.828实验中的bootloader链接地址与加载地址的思考
- 基于Swift的iOS应用程序开发:使用表格显示并控制数据(二):删除表格中的数据