UVALive 6439 贪心or字符串hash

6 篇文章 0 订阅
4 篇文章 0 订阅

在网上看到别人的一份题解,写的很好,关键两份代码也写的简洁明了,看了以后感到挺服的

http://blog.csdn.net/Mad_boys/article/details/47953779

题意:将给定字符串的部分子串用字符代替,使新得到的串成为回文串,并且得到的回文串长度最大。
如: S = `ABCADDABCA',让α=ABCA,β=DD,则新得到的回文串为αβα,长度为3。
让α= `A',β = `BC', γ = `D', 则S= α βα γγα βα ,长度为8.

大概讲一下思路吧,就是贪心,一个从前往后,另一个从后往前,然后遇到相同的就标记为1,最后判断一下中间部分就可以,暴力也能过,写hash当然也快的飞起

代码其实 还是模仿的链接里的那个,因为真的很好,另一种字符串hash的也可以看他的,也很清晰

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define maxn 50005
char str[maxn];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int T;
	scanf("%d", &T);
	for (int kase = 1; kase <= T; ++kase)
	{
		scanf("%s", str);
		int ans = 0;
		int len = strlen(str);
		string l = "", r = "";
		for (int i = 0; i <= (len - 1) / 2; ++i)
		{
			l = l + str[i];
			r = str[len - 1 - i] + r;
			if (l == r&&i != len - 1 - i)
			{
				ans += 2;
				l.clear();
				r.clear();
			}
			if (i == (len - 1) / 2 && !l.empty())
			{
				++ans;
			}
		}
		printf("Case #%d: %d\n", kase, ans);
	}
	//system("pause");
	//while (1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值