【水题】USACO Calf Flac

进入USACO要注册才能看题: http://train.usaco.org/usacogate

题目:【翻译版、是别处的网站】http://www.wzoi.org/usaco/13%5C206.asp

SAMPLE INPUT (file calfflac.in)
Confucius say: Madam, I'm Adam.

SAMPLE OUTPUT (file calfflac.out)
11
Madam, I'm Adam


水题啊…………



/*
ID: 1006100071
PROG: calfflac
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <iomanip>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

char buf[20005], str[20005];
int map[20005];

int main()
{
	/*freopen ("calfflac.in", "r", stdin);
	freopen ("calfflac.out", "w", stdout);*/
	char tp[85];
	int len, i, j, maxs = 0, start = 0, end = 0, k = 0;
	while (fgets (tp, 85, stdin))   //fgets可以把换行符也读进tp
		strcat (buf, tp);    //由于是一段话,所以要不断读取
	len = strlen (buf);
    //************************回文不理会标点以及大小写
	for (i = 0; i < len; i++)
	{
		if (!isalpha(buf[i]))
			continue;
		map[k] = i;    //str的第k个字符映射到原串的第i个字符
		if (isupper(buf[i]))
			str[k++] = tolower (buf[i]);
		else str[k++] = buf[i];
	}
    //************************找最大回文maxs,以及回文开端start和回文末端end
	for (i = 0; i < k; i++)    //枚举回文中点
	{
		for (j = 0; ; j++)    //回文长度为奇数
		{
			if (i - j < 0 || i + j > k || str[i-j] != str[i+j])
			{//超界或不相等都会使回文中断, 下面同理

				if (maxs < 2 * j - 1)
				{
					maxs = 2 * j - 1;
					start = i - j + 1;
					end = i + j - 1;
				}
				break;
			}
		}
		for (j = 0; ; j++)    //回文长度为偶数
		{
			if (i - j < 0 || i + j + 1 >= k || str[i-j] != str[i+j+1])
			{   
				if (maxs < 2 * j)
				{
					maxs = 2 * j;
					start = i - j + 1;
					end = i + j;
				}
				break;
			}
		}
	}
	printf ("%d\n", maxs);
	for (i = map[start]; i <= map[end]; i++)//输出原串,所以要映射到原串的开端和末端
		printf ("%c", buf[i]);
	printf ("\n");
	return 0;
}

你可能感兴趣的:(C++,c,算法,编程语言,ACM)