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的邮票,可以推出 ...
随机推荐
- Hadoop生态圈-HBase性能优化
Hadoop生态圈-HBase性能优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- python---协程理解
推文:python---基础知识回顾(七)迭代器和生成器 推文:Python协程深入理解(本文转载于该文章) 从语法上来看,协程和生成器类似,都是定义体中包含yield关键字的函数.yield在协程中 ...
- Shell记录-Shell命令(find)
Linux中的find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花 ...
- 我购买byd的几点逻辑
1.伯克希尔哈撒韦长期看好byd不是无道理的,每次转型都是那么的成功,说明管理层很好. 2.2015年6月员工持股计划均价55元,目前48元. 3.新能源汽车龙头. 4.云轨解决了小城市建设地铁过于浪 ...
- 六、强大的 Stream API
一.了解 Stream Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一个则是 Stream API(java.util.stream.*).Stream 是 Java8 中处 ...
- 10 款非常棒的CSS代码格式化工具推荐
如果你刚开始学习CSS,这意味着你的很多代码或结构可能需要优化,比如你可能过多使用了类.添加了多余的间隔或空行等等,这将导致代码臃肿.混乱,可读性和执行效率将大大降低. 本文为你整理了几款CSS工具, ...
- 深度优先搜索(DFS)----------------Tju_Oj_3517The longest athletic track
这个题主要考察对树的操作,主要思想是DFS或者BFS,其次是找树的直径方法(既要运用两次BFS/DFS),最后作为小白,还练习了vector的操作. DFS框架伪码: bool DSF(Node on ...
- 【leetcode 简单】 第七十八题 Nim游戏
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你 ...
- 【译】第二篇 Integration Services:SSIS数据泵
本篇文章是Integration Services系列的第二篇,详细内容请参考原文. 简介SSIS用于移动数据.数据流任务提供此功能.因为这个原因,当介绍SSIS时我喜欢从数据流任务开始.数据流任务的 ...
- 关于java线程锁synchronized修饰普通方法与静态方法的区别
最近研究线程方面问题,关于这个synchronized锁修饰的问题,先是修饰普通方法,然后通过两个线程,各自执行自己对象的锁,发现方法执行互不影响,代码如下: private static int n ...