Ball Blasting Game UVA - 12378 (马拉车)
2017-09-19 20:21
288 查看
题意:给一个字符串,然后可以选择一个字符消除,消除的位置两边会合并起来,当合并起来的相同字符大于等于二,则可以将相同字符消除,消除的位置两边可以继续合并,直到不能合并为止。问选择一个字符消除,最多可以消除几次
分析:先将相邻相同的写成一个字符,然后用马拉车求出最长回文串长度P,ans=(P+1)/2;
分析:先将相邻相同的写成一个字符,然后用马拉车求出最长回文串长度P,ans=(P+1)/2;
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <cstdio> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) const int maxn = 220000+10; char s1[maxn],s[maxn]; char s2[maxn]; int p[maxn]; int first(char *s1,char *s2) { int len=1,i=0; s2[0]='$'; while(s1[i]!='\0') { s2[len++]='#'; s2[len++]=s1[i++]; } s2[len++]='#'; return len; } void Manacher(int *p,char *str,int len) { memset(p,0,sizeof(p)); int mx=0,id=0; for(int i=0;i<len;i++) { p[i]=mx>i?min(p[2*id-i],mx-i):1; while(str[i+p[i]]==str[i-p[i]]) p[i]++; if(i+p[i]>mx) { mx=i+p[i]; id=i; } } } void init() { mem(s,'\0');mem(s1,'\0');mem(s2,'\0'); } int main() { int n;scanf("%d",&n); while(n--) { init(); scanf("%s",s); int len=strlen(s),h=0; s1[h++]=s[0]; for(int i=1;i<len;i++) if(s[i]!=s[i-1]) s1[h++]=s[i]; len=first(s1,s2); Manacher(p,s2,len); int ans=1; for(int i=0;i<len;i++) ans=max(ans,p[i]); printf("%d\n",ans/2); } return 0; }
相关文章推荐
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- Ball Blasting Game UVA - 12378 (马拉车)
- 消消乐 Manacher裸题 UVA 12378 Ball Blasting Game
- UVA 12378 Ball Blasting Game 【Manacher回文串】
- UVA 12378 Ball Blasting Game
- uva 1045 - The Great Wall Game(二分图匹配)
- UVALive 6693 - Flow Game(点在线段上判断)
- bfs UVA 1063 Marble Game
- UVA 10891 Game of Sum(区间DP)
- UVALive 5790 Ball Stacking 解题报告
- UVA 1378 A Funny Stone Game (经典博弈)
- hdu 4605 Magic Ball Game(离线+树状数组)
- [UVA 10891] Game of Sum
- uva 10400 - Game Show Math