Timus 1297. Palindrome
来源:互联网 发布:飞控pid算法 编辑:程序博客网 时间:2024/06/11 02:23
题目:Palindrome
题意:给出一段字符串,求其字串中的最长的回文串、
解法:枚举回文串的中间点,再往两边搜索,时间为O(n^2)
#include <iostream>#include <string>using namespace std;char s[2000];int n = 0;int main() { char c; while ((c = getchar()) != EOF && c != '\n') { s[++n] = c; } int ans = 0, l = 0; for (int i = 1; i <= n; i++) { int x = i, y = i, t = -1; while (x > 0 && y <= n && s[x] == s[y]) { t += 2; if (t > ans) { ans = t; l = x; } x--; y++; } x = i; y = i+1; t = 0; while (x > 0 && y <= n && s[x] == s[y]) { t += 2; if (t > ans) { ans = t; l = x; } x--; y++; } } for (int i = l; i <= l+ans-1; i++) cout << s[i]; cout << endl;}
0 0
- Timus 1297. Palindrome
- timus 1297 Palindrome//后缀数组,求最长公共回文字
- Timus OJ 1354. Palindrome. Again Palindrome(manacher变形)包尾部最长回文串
- 1297. Palindrome
- timus 1017
- timus 1073
- ural 1297. Palindrome
- ural 1297. Palindrome dp
- palindrome
- Palindrome
- Palindrome
- Palindrome
- Palindrome
- Palindrome
- Palindrome
- Palindrome
- palindrome
- Palindrome
- 基础级-phpDocumentor
- Maven和MyBatis整合时,漏掉mapper的xml文件和其他资源文件
- Codewars-Javascript训练手册:数组(上)
- 2016/04/15
- git web的搭建
- Timus 1297. Palindrome
- Chrome扩展程序二次开发
- 8086汇编语言 debug 示例 R命令,查看CPU的内容
- 2016/04/17
- Bom,Dom
- webui78一个小巧高效灵活的web前端UI组件库,包括表格、树、表单验证、图片按钮、对话框、面板等组件
- ios面试题1
- WifiDog实际应用注意的问题
- 多线程学习12-GCD实现单例模式