【USACO 1.3.3】回文串
【題目描述】
据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去寻找这些牛制造的奇观(最棒的回文)。
在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母'A'-'Z'和'a'-'z'。要你寻找的最长的回文的文章是一个不超过20,000个字符的字符串。我们将保证最长的回文不会超过2,000个字符(在除去标点符号、空格之前)。
【格式】
INPUT FORMAT: (file calfflac.in)
输入文件不会超过20,000字符。这个文件可能一行或多行,但是每行都不超过80个字符(不包括最后的换行符)。
OUTPUT FORMAT: (file calfflac.out)
输出的第一行应该包括找到的最长的回文的长度。
下一行或几行应该包括这个回文的原文(没有除去标点符号、空格),把这个回文输出到一行或多行(如果回文中包括换行符)。
如果有多个回文长度都等于最大值,输出最前面出现的那一个。
【分析】
直接模擬的話數據量實在是太大了,肯定會超時。
我們需要還一個角度來想,從第i個位置出發,向兩邊推,這是根據回文串的定義來做,需要注意的是,也要注意i,i+1再往兩邊推的情況。
同時還有一個要注意的是,枚舉可以直接從當前的最優長度來枚舉,這樣速度會有很大的提升。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
const int maxl=+;
using namespace std;
//注意:這裏d用來記錄位置
char a[maxl],b[maxl];
int d[maxl];
char change(char t)
{
//將大寫轉換爲小寫
if (t<'a') return (char)(t+);
return t;
}
bool check(int from,int to);
int main()
{
char temp;
//文件操作
freopen("calfflac.in","r",stdin);
freopen("calfflac.out","w",stdout);
memset(a,,sizeof(a));//a用來保存原文
memset(b,,sizeof(b));//b用來對比回文串
int point_1=,point_2=;
//讀入+轉換
while (scanf("%c",&temp)!=EOF)
{
a[point_1]=temp;
if ((temp>='a' && temp<='z') || (temp>='A' && temp<='Z'))
{
b[point_2]=change(temp);
d[point_2]=point_1;//記住同時要記錄位置
point_2++;
}
point_1++;
} int ans=,from=,to=,i,j;
for (i=;i<point_2;i++)
{
for (j=ans/;j<=min(i,min(point_2-i,));j++)
{
if (check(i-j,i+j))
{
if (*j+>ans)
{
ans=*j+;
from=d[i-j];
to=d[i+j];
}
}
else break;
}
for (j=ans/;j<=min(i,min(point_2-i,));j++)
{
if (check(i-j,i+j+))
{
if (*j+>ans)
{
ans=*j+;
from=d[i-j];
to=d[i+j+];
}
}
else break;
}
}
//輸出原文
printf("%d\n",ans);
for (i=from;i<=to;i++)
printf("%c",a[i]);
return ;
}
bool check(int from,int to)
{
int i;
for (i=from;i<=((from+to)/)+;i++)
if (b[i]!=b[from+to-i]) return ;
return ;
}
【USACO 1.3.3】回文串的更多相关文章
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- bzoj 3676 回文串 manachar+hash
考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...
- BZOJ 3676: [Apio2014]回文串
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2013 Solved: 863[Submit][Status ...
- SPOJ - PLSQUARE Palin Squar(hash+回文串)
题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
随机推荐
- Python操作Excel_输出所有内容(包含中文)
python 2.7.5代码: # coding=utf-8 import sys import xlrd data=xlrd.open_workbook('D:\\menu.xls') table ...
- div+css模式编写html静态网页例子_仿照网页制作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bellman-Bord(贝尔曼-福特)
include const int inf=0x3f3f3f3f; int main() { int m,n; scanf("%d%d",&n,&m); int u ...
- [Locked] Find the Celebrity
Find the Celebrity Suppose you are at a party with n people (labeled from 0 to n - 1) and among them ...
- [Locked] Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- python关键字、转义符和字符串格式化
最近在学learn python the hard way,学习到第37章,进行了关于关键字.转义符和字符串格式化的总结.看手头上的中文版没有及时更新.于是就把这些翻译过来,以作查阅. 关键字: 关键 ...
- [转]浏览器如何和Web服务器通信
http://hi.baidu.com/ywqme/item/b5297014b2e58f4e6826bb74 概述 普通网民打开网页,访问网站,并不需要了解所谓HTTP协议.作为软件工程师,了解一下 ...
- poj2245
题目分析: 抽奖 在德国乐透你需要从49个数(1,2,3......49)里面选择6个数,玩德国乐透一个流行的策略-尽管他不会增加你获奖的机会-选择一个子集S包含k(k>6)个数从这49个数字里 ...
- (组合数学3.1.1.2)UVA 10098 Generating Fast(使用字典序思想产生所有序列)
/* * UVA_10098.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> # ...
- python-用户登录小程序
算是第一篇博客吧~哈哈哈 虽然说是为了完成作业,不过以后估计会常来分享.首先说一下下边这个程序的基本功能.毕竟是第一次写python程序还是有点小激动和满满的成就感的,下边这个程序: 1.输入不存在的 ...