Gym_101350I_Mirrored String II_回文字符串(马拉车Manacher)
来源:互联网 发布:中国域名注册官网 编辑:程序博客网 时间:2024/06/16 21:19
Note: this is a harder version of Mirrored string I.
The gorillas have recently discovered that the image on the surface of the water is actually a reflection of themselves. So, the next thing for them to discover is mirrored strings.
A mirrored string is a palindrome string that will not change if you view it on a mirror.
Examples of mirrored strings are "MOM", "IOI" or "HUH". Therefore, mirrored strings must contain only mirrored letters {A, H, I, M, O, T, U, V, W, X, Y} and be a palindrome.
e.g. IWWI, MHHM are mirrored strings, while IWIW, TFC are not.
A palindrome is a string that is read the same forwards and backwards.
Given a string S of length N, help the gorillas by printing the length of the longest mirrored substring that can be made from string S.
A substring is a (possibly empty) string of characters that is contained in another string S. e.g. "Hell" is a substring of "Hello".
The first line of input is T – the number of test cases.
Each test case contains a non-empty string S of maximum length 1000. The string contains only uppercase English letters.
For each test case, output on a line a single integer - the length of the longest mirrored substring that can be made from string S.
3IOIKIOOIROQWOWMAN
413
题意:给你一串字符串,求这串字符里由限定的字符所组成的字符串中最长的回文串的长度。
解:马拉车直接做,只是需要处理下非限定的字符。
我是将符合规定的字符一个个拆开进行求回文串长度,比较遍历后找出最大的值输出。毕竟字符串长度才1000。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#define MANX 1101using namespace std;char str1[MANX],str0[MANX],str[MANX<<1];int p[MANX<<1],len;void start(){ str[0]='$',str[1]='#'; len=strlen(str0); for(int i=0;i<len;i++) { str[i*2+2]=str0[i]; str[i*2+3]='#'; } str[len*2+2]='\0';}int solve(){ memset(p,0,sizeof(p)); int mx=0,id,ans=1; for(int i=1;i<len*2+2;++i) { if(mx>i)p[i]=min(p[2*id-i],mx-i); else p[i]=1; for(;str[i-p[i]]==str[i+p[i]];p[i]++); if(p[i]+i>mx)mx=p[i]+i,id=i; if(p[i]>ans)ans=p[i]; } return --ans;}//A, H, I, M, O, T, U, V, W, X, Yint main(){ int t; scanf("%d",&t); while(t--) { memset(str,0,sizeof(str)); memset(str1,0,sizeof(str1)); memset(str0,0,sizeof(str0)); scanf("%s",str1); int len1=strlen(str1); len=0;int ans=-99999999; for(int i=0;i<len1;i++) { if(str1[i]=='A'||str1[i]=='H'||str1[i]=='I'||str1[i]=='M'||str1[i]=='O'||str1[i]=='U'||str1[i]=='V' ||str1[i]=='W'||str1[i]=='X'||str1[i]=='Y'||str1[i]=='T') { str0[len]=str1[i];//将符合规定的字符串给待处理的str0; len++; } else { start(); ans=max(ans,solve()); memset(str0,0,sizeof(str0));//清零重新处理 memset(str,0,sizeof(str)); len=0; } } start();//最后处理下 ans=max(ans,solve()); printf("%d\n",ans); } //cout << "Hello world!" << endl; return 0;}
- Gym_101350I_Mirrored String II_回文字符串(马拉车Manacher)
- 最长回文字符串算法-Manacher’s Algorithm-马拉车算法
- hihoCoder-1032 - 最长回文子串(Manacher 马拉车)
- 最长回文字符串(马拉车算法)
- 1040. Longest Symmetric String (25)【最长回文子串-马拉车(manacher算法)——PAT (Advanced Level) Practise
- 马拉车(manacher)算法——最长回文(hdu3068)
- POJ-3974-Palindrome- Manacher 马拉车算法(On寻找最长回文串)
- Manacher算法--O(n)回文子串算法(马拉车算法)
- 【LeetCode】Longest Palindromic Substring(最长回文子串1)[马拉车Manacher算法]
- hihoCoder 最长回文子串(manacher算法:马拉车)
- Manacher马拉车算法求最长回文子串
- Manacher(回文字符串)
- 浅谈Manacher(马拉车)算法
- 马拉车算法(Manacher)详解
- Manacher(马拉车)算法详解
- String——回文字符串manacher算法
- 最长回文子串(马拉车)
- Manacher马拉车算法总结
- CNN常见问题总结
- 上传应用到Google Play
- JS设计模式二:工厂模式
- JVM 内存模型
- 有用的文章链接
- Gym_101350I_Mirrored String II_回文字符串(马拉车Manacher)
- JS设计模式三:桥接模式
- ecshop怎样解决重复订单问题
- 父元素跟随子元素margin-top
- mongodb 笔记
- HashMap Hashtable区别
- 打造网络营销利器-第7篇-图片优化
- C# SideBar控件
- 高斯消元法(Gauss Elimination) 分析 & 题解 & 模板——czyuan原创