Calf Flac
To make the programming easier, we keep two copies of the text: the original, and one with the punctuation stripped out. We find the biggest palindrome in the latter copy, and then figure out which part of the original it corresponds to.
To find the biggest palindrome in the alphabetic text, we look, for each letter in the text, at the biggest palindrome centered on that letter (in the case of an odd length palindrome) or just to the right of that letter (in the case of an even length palindrome).
There are 20,000 letters, and we are assured that no palindrome is more than 2,000 letters long, and we search for both even and odd palindromes, for a total of about 20,000*2,000*2 = 80,000,000 operations, which is perfectly reasonable within the time limit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
char fulltext[21000];
char text[21000];
char *pal;
int pallen;
void
findpal(void)
{
char *p, *fwd, *bkwd, *etext;
int len;
etext = text+strlen(text);
for(p=text; *p; p++) {
/* try palindrome with *p as center character */
for(fwd=bkwd=p; bkwd >= text && fwd < etext && *fwd == *bkwd;
fwd++, bkwd--)
;
bkwd++;
len = fwd - bkwd;
if(len > pallen) {
pal = bkwd;
pallen = len;
}
/* try palindrome with *p as left middle character */
for(bkwd=p, fwd=p+1;
bkwd >= text && fwd < etext && *fwd == *bkwd; fwd++, bkwd--)
;
bkwd++;
len = fwd - bkwd;
if(len > pallen) {
pal = bkwd;
pallen = len;
}
}
}
void
main(void)
{
FILE *fin, *fout;
char *p, *q;
int c, i, n;
fin = fopen("calfflac.in", "r");
fout = fopen("calfflac.out", "w");
assert(fin != NULL && fout != NULL);
/* fill fulltext with input, text with just the letters */
p=fulltext;
q=text;
while((c = getc(fin)) != EOF) {
if(isalpha(c))
*q++ = tolower(c);
*p++ = c;
}
*p = '\0';
*q = '\0';
findpal();
fprintf(fout, "%d\n", pallen);
/* find the string we found in the original text
by finding the nth character */
n = pal - text;
for(i=0, p=fulltext; *p; p++)
if(isalpha(*p))
if(i++ == n)
break;
assert(*p != '\0');
/* print out the next pallen characters */
for(i=0; i<pallen && *p; p++) {
fputc(*p, fout);
if(isalpha(*p))
i++;
}
fprintf(fout, "\n");
exit(0);
}
此题如果能够想到暴力,那么此题容易错的地方就在读入上,要记得是以文件结束符EOF结束输入的,而不是以'\n',因为这个原因而WA了几次。
附我的代码:
/*
ID: xxfz014
PROG: calfflac
LANG: C++
*/
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 20010;
char org[maxn];
struct node
{
int index;
char ch;
};
node s[maxn];
int len;
bool isword(char c)
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z')) return true;
return false;
}
int main()
{
freopen("calfflac.in","r",stdin);
freopen("calfflac.out","w",stdout);
int i=0,len=0;
while(scanf("%c",&org[len++])!=EOF);
//printf("%d\n",len);
//puts(org);
for(int j=0;j<len;j++)
{
if(isword(org[j]))
{
if(org[j]>='A'&&org[j]<='Z') s[i].ch=org[j]-'A'+'a';
else s[i].ch=org[j];
s[i++].index=j;
}
}
len=i;
int maxv=0,left,right;
for(i=0;i<len;i++)
{
int v=0,a=i,b=i;
//偶数
for(int r=i,l=i-1;r>=0&&l>=0&&r<len&&l<len;r++,l--)
{
if(s[r].ch==s[l].ch) {v+=2;a=l;b=r;}
else break;
}
if(v>maxv) {maxv=v;right=b;left=a;}
//奇数
v=1,a=i,b=i;
for(int r=i+1,l=i-1;r>=0&&l>=0&&r<len&&l<len;r++,l--)
{
if(s[r].ch==s[l].ch) {v+=2;a=l;b=r;}
else break;
}
if(v>maxv) {maxv=v;right=b;left=a;}
}
printf("%d\n",maxv);
for(i=s[left].index;i<=s[right].index;i++) printf("%c",org[i]);
printf("\n");
return 0;
}
分享到:
相关推荐
Usaco中第一章中的一道题,挺有意思的。我的程序中间用到了折半查找,牺牲空间求效率
小牛工作室装备Calf Studio Gear是LINUX操作系统下用于LV2和JACK环境的音频插件包。 该套件包含许多效果(延迟,调制,信号处理,滤波器,均衡器,动态效果,失真和母带效果),乐器(SF2播放器,风琴模拟器和单音...
J2ME中文教程_calf1.01a(游戏制作),喜欢的童鞋可以下载看看
电化学和光谱法在中性pH条件下对Al(III)促进双链小牛胸腺DNA变性的研究,章福平,曹庆,本文主要研究了在悬汞电极上用微分脉冲伏安法、拉曼光谱以及圆二色光谱法研究双链小牛胸腺DNA(ds-DNA)与Al(III)之间的相互作用...
Dependence of surface-enhanced Raman scattering (SERS) from Calf thymus DNA on anions is investigated. With the silver colloid, the bands at 732, 960 and 1333 cm-1 for adenine (A), 1466 cm-1 for ...
Calf是一个简单的CGI应用程序,用于生成比您的Web服务器更凉爽的文件列表。 它按年和月组织文件,因为它主要用于浏览档案。 它需要一个特定的层次结构,即/YYYY/MM/something/file 。 例如/2014/01/pic/lipsum.jpg ...
比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素。换言之,返回两个数组的差异。(6种解题方法) ... [1, "calf", 3, "piglet"], [1, "calf", 3, 4] 应该返回 ["piglet", 4]。
We used Raman spectroscopy to study the conformational changes of DNA induced by Cd2+ ions in different Cd2+ concentrations solution. The experimental results show that when the Cd^(2+)/PO^(-)_(2) ...
见下表: 总称雄性名称雌性名称小动物名称肉 chicken cock rooster hen chick chicken duck drake duck duckling goose gander goose gosling horse stallion mare foal cattle bull/ox cow calf beef pig boar sow ...
常见动物的英文名称horse马mare母马colt, foal马驹,小马pony矮马thoroughbred纯种马mustang野马mule骡ass, donkey驴ox牛buffalo水牛bull公牛cow母牛calf小牛,牛犊bullock, steer小阉牛heifer小母牛pig, swine猪boar...
giant salamander 娃娃鱼 C cat 猫 crab 螃蟹 camel 骆驼 cow 母牛 calf 小牛 cock 公鸡 chicken 小鸡 crocodile 鳄鱼 少儿动物英语单词全文共7页,当前为第2页。少儿动物英语单词全文共7页,当前为第2页。cricket ...
host.add_plugin ("http://calf.sourceforge.net/plugins/Compressor" ,"compressor" .to_owned (), std:: ptr::null_mut ()).expect ("Lv2hm: could not add plugin" ); host.add_plugin (...
In this paper, the interaction between mitoxantrone and calf thymus DNA is investigated by Raman and fluorescence spectroscopies, and the binding site of mitoxantrone to calf thymus DNA is explored....
RITA DSL 这是一种宽松地基于语言的语言,专注于编写手动语言规则,该规则可...lengths = {"short", "long", "calf-length", "knee-length"} fabric_types = {"soft", "airy", "crinkled"} fabrics = {"velour", "chi
Kudu小牛Kudu Calf 用于从父 Kudu 站点部署子站点文档查看执照项目治理这个项目在保护伞下。
Function_CalF1.py (F1函数的计算) 负样本影响问题 测试产生 '候选集'的过程中,发现虽然只用子集,但是一早上起来,数据量还是把硬盘给爆了,经测试,代码应该没有产生大问题。估计是负样本太多,过多的想预测没有...
#########扁平蝴蝶######### -fbfly是#########削片机######### -router.algorithm DR_FLIT_SWITCHED_CALF -edge_loop true -meshEjectTrial 2 meshEjectTrial是弹出端口的宽度。 #########VC缓冲######### -router...
java web开发应用.类似人人空间的设计,比较简单,有说说,日志,相册,站内信,留言,互相访问等功能.登录页面设计类似人人网,数据库采用Oracle.
针对市面上大量的海康摄像头的驱动库文件,覆盖海康绝大多数产品
好用的Xshell5,方便用户在linux系统中进行命令行操作,是linux服务器的不二选择