【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 ...
随机推荐
- matlab制造一个64*64的仿真数据
fid = fopen('test_001.img','w'); r=random('Normal',100,0,64,64); z=random('Uniform',0,5,64,64); %%%% ...
- 【转】Java ConcurrentModificationException异常原因和解决方法
原文网址:http://www.cnblogs.com/dolphin0520/p/3933551.html Java ConcurrentModificationException异常原因和解决方法 ...
- SCU 4440 Rectangle 2015年四川省赛题
题目链接:http://acm.scu.edu.cn/soj/problem/4440/ 题目大意:给一个n*m的方格,求周长小于等于k的矩形有多少个. 解题思路:我之前直接暴力,显然超时,所以后来发 ...
- DNA Sequence - POJ 2778(AC自动机+矩阵乘法)
题目大意:DNA序列是有 ATGC 组成的,现在知道一些动物的遗传片段有害的,那么如果给出这些有害的片段,能否求出来所有长度为 N 的基因中有多少是不包含这些有害片段的. 分析:也是断断续续做了一 ...
- Clairewd’s message - HDU 4300(next[]运用)
题目大意:给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能. 分析:题 ...
- js标题文字向上滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- JS常用扩展
// 清除两边的空格 String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ''); }; // 合并多 ...
- jni java和C之间的值传递(int String int[])
我们通过jni调用C代码不可能每次只是去调一个方法,通常,我们需要传递一些值过去. 例如,播放电影,那就肯定需要你把电影的 url给 C的播放器吧,等等. 接下来就看一看怎么去传递这些值: 首先是最简 ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 论C++与三国
Scott Meyers曾说过,C++语言是一个语言联邦.C++包含面向过程,面向对象,泛型编程编程思想.现在C++11有加了一堆新特性,语言联邦更为庞大. 程序员们.常常挑起语言之争,甚至连大师级人 ...