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的邮票,可以推出 ...
随机推荐
- EChart介绍和使用
一.简单介绍 Echart是百度研发团队开发的一款报表视图JS插件,功能十分强大,使用内容做简单记录:(EChart下载地址 http://echarts.baidu.com/download.htm ...
- HTTP协议(4):CGI
CGI接口原理及实现(2012-12-7 Over) 1.CGI定义: CGI(CommonGateway Interface)是HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须 ...
- Windows下php,mysql,apache相关安装与配置,完善中…
PHP 的安装 由于php是一个zip文件(非install版),安装较为简单解压就行.把解压的 php5.2.1-Win32重命名为 php5.并复制到安装盘目录下.例如安装路径为 c:\php5 ...
- [洛谷P4492] [HAOI2018]苹果树
洛谷题目链接:[HAOI2018]苹果树 题目背景 HAOI2018 Round2 第一题 题目描述 小 C 在自己家的花园里种了一棵苹果树, 树上每个结点都有恰好两个分支. 经过细心的观察, 小 C ...
- 视差插件parallarx
github上的demo,自己拿来改了改. <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- 解决IE6中 PNG图片透明的终极方案-八种方案!
“珍惜生命,远离IE6”,IE6中的bug令很多Web前端开发人员实为头疼,因此不知道烧了多少脑细胞,在众多的Bug中最令人抓狂的就是IE对png图片的不支持,导致设计师和重构师放弃了很多很炫的效果, ...
- 面向对象__construct(构造方法)、__destruct(析构方法)
//1.创建子类的时候构造函数会直接取创建函数时传的参数,例如下面例子中构造函数直接取了new Person("张三","男", 20);里面的3个参数. // ...
- 【leetcode 简单】 第七十九题 区域和检索 - 数组不可变
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数 ...
- 【译】第九篇 Replication:复制监视器
本篇文章是SQL Server Replication系列的第九篇,详细内容请参考原文. 复制监视器允许你查看复制配置组件的健康状况.这一篇假设你遵循前八篇,并且你已经有一个合并发布和事务发布.启动复 ...
- opencv 摄像头
VideoCapture cap(); if(!cap.isOpened()) ; Mat frame, edges; namedWindow(); for(;;) { cap >> fr ...