USACO section 1.3.3
2012-04-11 22:34
295 查看
1. 输入带回车符,空格,标点符号的字符串:
2. 判断是不是回文
3. 以下是代码:
4. 官方代码:
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.
while (fin >> temp) { str += temp; str += " "; } unsigned int len = str.size(); str.resize(len - 1);
2. 判断是不是回文
3. 以下是代码:
/* ID: dollar41 PROG: calfflac LANG: C++ */ #include<iostream> #include<fstream> #include<string.h> #define maxn 20010 using namespace std; char str1[maxn],str2[maxn*2]; int n,l1,mid,len; int nxt[maxn*2]; void Manacher () { int mx=0,id; len=-1; memset(nxt,0,sizeof(nxt)); for(int i=1; i<l1; i++) { if(mx>i) nxt[i]=min(nxt[2*id-i],mx-i); else nxt[i]=1; for(; str2[i-nxt[i]]==str2[i+nxt[i]]; nxt[i]++); if(mx<i+nxt[i]) { mx=i+nxt[i]; id=i; } if(len<nxt[i]-1) { len=nxt[i]-1; mid=i; } } } void pre() { str2[0]='$'; int i=0,t=0,k=1; while(str1[i]!='\0') { if(t) { int flag=0; while(1) { if((str1[i]>='A'&&str1[i]<='Z')|| (str1[i]>='a'&&str1[i]<='z')) break; if(str1[i]=='\0') { flag=1; break; } i++; } if(flag) break; str2[k++]=str1[i++]; } else str2[k++]='#'; if(str2[k-1]>='A'&&str2[k-1]<='Z') str2[k-1]+=32; t^=1; } str2[k++]='#'; str2[k]='\0'; l1=k; } void print(int s) { int count=0,flag=0; for(int i=s; str1[i]!='\0'; i++) { if((str1[i]>='A'&&str1[i]<='Z')|| (str1[i]>='a'&&str1[i]<='z')) count++; printf("%c",str1[i]); if(count==len) break; } printf("\n"); } int main() { freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout); char str[maxn]; while(gets(str)) { strcat(str1,"\n"); strcat(str1,str); } pre(); //printf("%s\n",str2); Manacher(); printf("%d\n",len); int count=0,s=(mid-len)/2+1; for(int i=0; str1[i]!='\0'; i++) { if((str1[i]>='A'&&str1[i]<='Z')|| (str1[i]>='a'&&str1[i]<='z')) count++; if(count==s) { print(i); break; } } return 0; }
4. 官方代码:
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); }
相关文章推荐
- USACO section 1.3.3
- USACO Section 1.3.3 Barn Repair
- USACO section2.2 Party Lamps题解&思路
- USACO Section 4.2 The Perfect Stall(二分图匹配)
- USACO section3.1 Humble Numbers题解&代码
- USACO section 1.5 Superprime Rib(深搜)
- USACO Section1.3 Combination Lock 解题报告
- USACO Section 5.1 Fencing the Cows(凸包)
- USACO Section 5.3 Milk Measuring (IDDFS+dp)
- USACO Section 1.5 Prime Palindromes(回文数+素数)
- USACO Section 3.2 Sweet Butter(SPFA)
- USACO section 2.4 Fractions to Decimals(小数处理)
- USACO section 3.3 Riding the Fences(欧拉通路的遍历,dfs)
- USACO Section 1.2.3 Transformations
- USACO-Section 1.5 Prime Palindromes(Miller-Rabin)
- USACO-Section 2.1 Healthy Holsteins (状态压缩DFS)
- USACO - Chapter1 Section 1.3 - Prime Cryptarithm
- USACO - Chapter2 Section 2.1 - Sorting a Three-Valued Sequence
- USACO-Section1.1 Your Ride Is Here [模拟]
- USACO-Section1.1 Greedy Gift Givers