1. 程式人生 > >USACO Section 1.3 : Calf Flac (calfflac)

USACO Section 1.3 : Calf Flac (calfflac)

pop name sizeof lib oid ring putc 空格 urn

題意:據說假設你給無限僅僅母牛和無限臺巨型便攜式電腦(有很大的鍵盤),那麽母牛們會制造出世上最優秀的回文。

你的工作就是去尋找這些牛制造的奇觀(最優秀的回文)。

在尋找回文時不用理睬那些標點符號、空格(但應該保留下來以便做為答案輸出),僅僅用考慮字母‘A‘-‘Z‘和‘a‘-‘z‘。要你尋找的最長的回文的文章是一個不超過20,000個字符的字符串。 我們將保證最長的回文不會超過2,000個字符(在除去標點符號、空格之前)。

暴力 註意一些細節 輸入字符中有回車啊之類的 wins下 輸完按回車按Ctrl+z再按回車即可了

ubuntu下是Ctrl+z

代碼:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010<<1
#define mod 1000000000+7
using namespace std;
void mys(int& res)
{
int flag=0;
char ch;
while(!(((ch=getchar())>=‘0‘&&ch<=‘9‘)||ch==‘-‘))
if(ch==EOF) res=INF;
if(ch==‘-‘) flag=1;
else if(ch>=‘0‘&&ch<=‘9‘) res=ch-‘0‘;
while((ch=getchar())>=‘0‘&&ch<=‘9‘) res=res*10+ch-‘0‘;
res=flag?-res:res;
}
void myp(int a)
{
if(a>9)
myp(a/10);
putchar(a%10+‘0‘);
}
/********************the end of template********************/
struct node{
int st, ed, lenth;
}s;
struct strtr{//副本
int pos;
char c;
}strt[20001];
char str[20001];
bool can(char x, char y){
if(x == y || (x - y == (‘a‘ - ‘A‘)) || (y - x == (‘a‘ - ‘A‘))) return true;
return false;
}
int main(){
int j, k;
char ch;
int top = 0, len = 0;
w((ch = getchar()) != EOF){
str[len++] = ch;
if(isalpha(ch)){
strt[top].c = ch;
strt[top++].pos = len-1;
}
}
s.lenth = -1;
for(int i = 0; i < top ; i ++){
if(strt[i].c != strt[i+1].c){
for(j = i-1, k = i+1; j>=0 && k<top; j--, k++){
if(!can(strt[j].c, strt[k].c )) break;
}
}
else{
for(j = i-1, k = i + 2; j>=0 && k<top; j--, k++){
if(!can(strt[j].c, strt[k].c )) break;
}
}
if(s.lenth < k - j - 1){
s.st = strt[j+1].pos;
s.ed = strt[k-1].pos;
s.lenth = k - j - 1;
}
}
cout<<s.lenth<<endl;
for(int i=s.st; i<=s.ed; i++)
cout<<str[i];
cout<<endl;
return 0;
}


USACO Section 1.3 : Calf Flac (calfflac)