UVALive 6439

来源:互联网 发布:数据产品经理入门 编辑:程序博客网 时间:2024/06/03 13:16

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

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 50005char 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;}

0 0