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的邮票,可以推出 ...
随机推荐
- 转:NSString / NSData / char* 类型之间的转换
1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname ...
- JS中双等号“==”使用情况
1.判断一个对象的值是否存在 var obj = {}; if(obj.a == null) { // todo } 2. 判断函数入参是否存在 function (a, b) { if(a == n ...
- MAC 下用 Common Lisp 调试 OpenGL 程序
MAC 下用 Common Lisp 调试 OpenGL 程序 环境搭建 运行环境: OSX 10.11.3 EI Capitan Common Lisp: SBCL 使用 SBCL, 首先要安装这几 ...
- H264编码profile & level控制
背景知识 先科普一下profile&level.(这里讨论最常用的H264) H.264有四种画质级别,分别是baseline, extended, main, high: 1.Baseli ...
- Django 2.0.1 官方文档翻译: 编写你的第一个 Django app,第五部分(Page 10)
编写你的第一个 Django app,第五部分(Page 10)转载请注明链接地址 我们继续建设我们的 Web-poll 应用,本节我们会为它创建一些自动测试. 介绍自动测试 什么是自动测试 测试是简 ...
- The Ph.D. Grind
The Ph.D. Grind A Ph.D. Student Memoir Summary The Ph.D. Grind, a 122-page e-book, is the first know ...
- 监控Elasticsearch的插件【check_es_system】
监控Elasticsearch的插件推荐 强大的shell脚本 #!/bin/bash ####################################################### ...
- 七、Kafka 用户日志上报实时统计之编码实践
一.数据生产实现 1.配置数据生产模块 项目基础配置所包含的内容,如下所示: •项目工程的文件配置 •集群连接信息配置 •开发演示 2.实现 Flume 到 Kafka 模块 实现 Flume 到 K ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- Elasticsearch技术解析与实战(一)基础概念及环境搭建
序言 ES数据架构的主要概念(与关系数据库Mysql对比) 集群(cluster) 集群,一个ES集群由一个或多个节点(Node)组成,每个集群都有一个cluster name作为标识.一下是我们的4 ...