最长回文子串(Mirrored String II)
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.
3
IOIKIOOI
ROQ
WOWMAN
4
1
3
题解:暴力呀。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#define PI acos(-1.0)
using namespace std;
bool judge(char arr)
{
if(arr=='A'||arr=='H'||arr=='I'||arr=='M'||arr=='O'||arr=='T'||arr=='U'||arr=='V'||arr=='W'||arr=='X'||arr=='Y')
return false;
return true;
}
int main()
{
int i,j,n,k,m,kk;
char str[];
scanf("%d",&n);
while(n--)
{
int ans=,pp;
scanf(" %s",&str);
for(i=;i<strlen(str);i++)
for(j=i;j<strlen(str);j++)
{
if(judge(str[j]))
break;
if(str[j]==str[i])
{
m=j;int sum=;
for(k=i;k<(i+j+)/;k++)
{
if(str[k]==str[m--])
sum++;
}
if(sum==(j-i+)/)
ans=max(ans,j-i+);
} }
printf("%d\n",ans); }
return ;
}
最新文章
- JS控制div跳转到指定的位置的解决方案总结
- 出现个Expression(str != NULL)
- Qt窗口的屏幕居中显示
- Codeforces Round #242 (Div. 2) B. Megacity
- Service(一)----->;简单计算
- C#操作office进行Excel图表创建,保存本地,word获取
- Swift学习笔记七
- http://nxlhero.blog.51cto.com/962631/1666250?plg_nld=1&;plg_uin=1&;plg_auth=1&;plg_nld=1&;plg_usr=1&;plg_vkey=1&;plg_dev=1
- 关于Arrays类总结
- Java吸收换行符
- GitHub 的分支创建与合并
- shell基础(转)
- List分组 用于客服对话分组场景
- Winform界面中实现菜单列表的动态个性化配置管理
- Python列表操作
- linux服务器ssh免密登录
- Linux网络那点事(CentOS、Ubuntu、Kali)
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
- day22 ramdom 模块
- 学习stm32 让我们一起回顾一下C语言吧