【題目描述】

据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去寻找这些牛制造的奇观(最棒的回文)。

在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母'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】回文串的更多相关文章

  1. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. bzoj 3676 回文串 manachar+hash

    考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...

  7. BZOJ 3676: [Apio2014]回文串

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2013  Solved: 863[Submit][Status ...

  8. SPOJ - PLSQUARE Palin Squar(hash+回文串)

    题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...

  9. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. linkedin和facebook的区别

    摘录一段百科(http://www.baike.com/wiki/LinkedIn)的文字: Linkedin - 特点 Linked是一个“高效”.“安全”并且“有商务价值”的“白领SNS提供商”: ...

  2. 杂题 UVAoj 10000 Longest Paths

      Longest Paths  It is a well known fact that some people do not have their social abilities complet ...

  3. ScrollView自动滑到底部

    // 自动滑动到底部 mScrollView.post(new Runnable() { @Override public void run() { mScrollView.fullScroll(Sc ...

  4. nginx在mac下的安装与基本操作

    1. 安装 brew install nginx(需要安装homebrew) 2. 执行  nginx 直接启动nginx服务 3. nginx -s  reload/stop 4. 配置地址 sud ...

  5. POJ 3280 Cheapest Palindrome(水题)

    题意:给定一个完全由小写字母组成的字符串s,对每个字母比如x(或a,b,c...z),在字符串中添加或者删除它分别需要花费c1['x']和c2['x']的代价,问将给定字符串变成回文串所需要的最少代价 ...

  6. Azkaban2配置过程

    Azkaban2配置过程 azkaban2所需环境:jdk1.6.ant.jetty.hadoop.ssl证书 通过http://azkaban.github.io/azkaban2/download ...

  7. OpenERP新手易犯错误之res.model

    接触OpenERP的人都感慨资料之少,尤其是XML中,出点错是相当郁闷的.尤其是新手.什么都别说了,有图有真相. 视图中关联模型name="model" ,而动作中name=&qu ...

  8. 设置IIS7文件上传的最大大小 maxAllowedContentLength,maxRequestLength

    当上传一个超过30M的文件时,服务器会重定向至404.13页面,报错如下: HTTP Error 404.13 - Not Found The request filtering module is ...

  9. Oracle EBS 入门

    Oracle EBS 入门Oracle EBS全称是Oracle 电子商务套件(E-Business Suit),是在原来Application(ERP)基础上的扩展,包含ERP(企业资源计划管理). ...

  10. boost库在工作(15)绑定器与函数对象之三

    前面已经可以优美地解决两个参数的函数给算法for_each调用了,但是又会遇到这样的一种情况,当需要三个参数或者三个以上的参数给算法for_each调用呢?从STL里的绑定器bind1st,显然是不行 ...