USACO 6.2 Calf Flac
Calf Flac
It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes. Your job will be to detect these bovine beauties.
Ignore punctuation, whitespace, numbers, and case when testing for palindromes, but keep these extra characters around so that you can print them out as the answer; just consider the letters `A-Z' and `a-z'.
Find the largest palindrome in a string no more than 20,000 characters long. The largest palindrome is guaranteed to be at most 2,000 characters long before whitespace and punctuation are removed.
PROGRAM NAME: calfflac
INPUT FORMAT
A file with no more than 20,000 characters. The file has one or more lines which, when taken together, represent one long string. No line is longer than 80 characters (not counting the newline at the end).
SAMPLE INPUT (file calfflac.in)
Confucius say: Madam, I'm Adam.
OUTPUT FORMAT
The first line of the output should be the length of the longest palindrome found. The next line or lines should be the actual text of the palindrome (without any surrounding white space or punctuation but with all other characters) printed on a line (or more than one line if newlines are included in the palindromic text). If there are multiple palindromes of longest length, output the one that appears first.
SAMPLE OUTPUT (file calfflac.out)
11
Madam, I'm Adam
————————————————题解
话说样例什么鬼……?
Manacher最长回文子串匹配算法,简单易写
就是USACO上isalpha函数不好用????
/*
ID: ivorysi
LANG: C++
PROG: calfflac
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
char str[],cmp[];
int l,cnt,pos[];
void init() {
char t[];
t[]='\0';
while(fgets(t+,,stdin)!=NULL) {
strcat(str+,t+);
//str[strlen(str+1)]='\0';
}
l=strlen(str+);
cmp[]='%';
cmp[++cnt]='@';
siji(i,,l) {
if(str[i]>='a' && str[i] <='z') {
cmp[++cnt]=str[i];
pos[cnt]=i;
cmp[++cnt]='@';
}
else if(str[i]>='A' && str[i] <='Z') {
cmp[++cnt]=str[i]-'A'+'a';
pos[cnt]=i;
cmp[++cnt]='@';
}
}
cmp[cnt+]='%';
cmp[cnt+]='\0';
}
int p[],id,mx;
void solve() {
init();
siji(i,,cnt) {
if(mx>i) {
p[i]=min(p[id*-i],mx-i+);
//要么就是对称的部分,如果它对称点长度超过了最大回文串的左边界,那就用右边界减去i
}
else {
p[i]=;
}
for(;cmp[i+p[i]]==cmp[i-p[i]];++p[i]) ;
if(mx<i+p[i]-) {
mx=i+p[i]-;
id=i;
}
}
id=;
siji(i,,cnt) {
if(p[id]<p[i]) id=i;
}
int ansl,ansr;
if(cmp[id+p[id]-]!='@') ansr=pos[id+p[id]-];
else ansr=pos[id+p[id]-];
str[ansr+]='\0';
if(cmp[id-p[id]+]!='@') ansl=pos[id-p[id]+];
else ansl=pos[id-p[id]+];
printf("%d\n%s\n",p[id]-,str+ansl);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("calfflac.in","r",stdin);
freopen("calfflac.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 6.2 Calf Flac的更多相关文章
- Calf Flac
1.3.3 Calf Flac Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 223 Solved: 42[Submit][Status][Forum] ...
- USACO 1.3.3 Calf Flac(Manacher算法)
Description 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文.你的工作就是去寻找这些牛制造的奇观(最棒的回文). 在寻找回文时不用理睬那些标 ...
- USACO Section 1.3 : Calf Flac (calfflac)
题意:据说假设你给无限仅仅母牛和无限台巨型便携式电脑(有很大的键盘),那么母牛们会制造出世上最优秀的回文. 你的工作就是去寻找这些牛制造的奇观(最优秀的回文). 在寻找回文时不用理睬那些标点符号.空格 ...
- USACO chapter1
几天时间就把USACO chapter1重新做了一遍,发现了自己以前许多的不足.蒽,现在的程序明显比以前干净很多,而且效率也提高了许多.继续努力吧,好好的提高自己.这一章主要还是基本功的训练,没多少的 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
随机推荐
- java 修饰符总结
java中的修饰符分为类修饰符,字段修饰符,方法修饰符.根据功能的不同,主要分为以下几种. 1.权限访问修饰符 public,protected,default,private,这四种级别 ...
- HashMap源码分析-基于JDK1.8
hashMap数据结构 类注释 HashMap的几个重要的字段 hash和tableSizeFor方法 HashMap的数据结构 由上图可知,HashMap的基本数据结构是数组和单向链表或红黑树. 以 ...
- webApp 页面适配布局
webApp 页面布局 1. 流式布局 概念: 流式布局是页面元素宽度按照屏幕分辨率进行适配调整,但是整体布局不变. 设计方法: 布局都是通过百分比来定义宽度,但是高度大都是用px固定的. 弊端: 虽 ...
- matlab中uigetfile命令的应用
matlab中uigetfile命令的应用 uigetfile命令的应用 此函数的用法为 [FileName,PathName,FilterIndex] = uigetfile(FilterSpec, ...
- Java并发编程原理与实战四十一:重排序 和 happens-before
一.概念理解 首先我们先来了解一下什么是重排序:重排序是指编译器和处理器为了优化程序性能而对指令序列进行重新排序的一种手段. 从Java源代码到最终实际执行的指令序列,会分别经历下面3种重排序,如下图 ...
- Ubuntu 15.04 双击运行 *.sh、*.py文件
源 起 之前一直在Windows下用AndoridStudio,今天试了一下在Linux系统Ubuntu 15.04中配置Android Studio: 过程和Windws下差不多,但是最后没有生成桌 ...
- R6—单变量正态性检验
方法不唯一 单变量正态检验主要的话包括以下这些 shapiro.test();#Shapiro-Wilk检验 library("nortest"); lillie.test() # ...
- Infinite Fraction Path(HDU6223 + bfs + 剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6223 题目: 题意: 给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点 ...
- 简明Python教程 ~ 随书笔记
本文是阅读<简明Python教程>所做的随书笔记,主要是记录一些自己不熟悉的用法,或者所看到的比较有意思的内容,本书英文版A Byte of Python, 中文译版 简明Python教程 ...
- app横竖屏切换
问题: 使用react编写的页面,编译后的页面文件打包成app安装后,在手机上显示时,初次横竖屏切换时会出现页面尺寸渲染问题,要跳到其它页面后才能恢复,如图: 由竖屏切换成横屏后页面出现很多空白. 解 ...