Mirrored String II

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”.

input

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.

output

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.

Example

Input
3
IOIKIOOI
ROQ
WOWMAN

Output
4
1
3

题目链接

题意
给你一个字符串,然后让你判断里面最长的回文子串的长度是多少。特殊要求:回文串必须是由题目上给的那个字母集合中的字母组成的才可以。
思路这个题没有什么太难的地方,直接从头到尾遍历就好了。先从前往后确定开头,然后再从后往前遍历,寻找回文串,在判断回文串的过程中,也要判断该子串中是否有题目所给字母集合之外的字母。然后保存回文串长度的最大值就好了。

#include <iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
using namespace std;
string s;
char ok[12] = {'A', 'H', 'I','M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'};
map<char,int> in;
bool is(int a,int b)//判断是否是回文串
{
    int p=b;
    for(int i=a;i<=(a+p)/2;i++)
    {
        if(in[s[i]]==0)
            return false;
        if(s[i]!=s[b])
            return false;
        b--;
    }
    return true;
}
int main()
{
    int T;
    for(int i=0;i<11;i++)
        in[ok[i]]++;//将题目所给字母放入map中,方便后面判断是否存在字母集合之外的字母
    scanf("%d",&T);
    while(T--)
    {
        cin>>s;
        int len=0;
        for(int i=0;i<s.size();i++)//从前往后遍历,确定回文子串的开头
        {
            if(in[s[i]]==0)
            continue;
            int p=s.size()-1;
            if(s.size()-i<=len)
                break;
            while(i<=p)//从后往前遍历,找到以s[i]为首的最长回文串
            {
                if(is(i,p))
                {
                    len=max(len,p-i+1);
                    break;
                }
                p--;
            }
        }
        cout<<len<<endl;
    }
}
### 马拉车算法 (Manacher's Algorithm) 的 C++ 实现 马拉车算法的核心在于通过对原字符串进行预处理,使得无论是奇数长度还是偶数长度的回文都可以被统一处理为奇数长度的形式[^4]。以下是完整的 C++ 实现代码: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // Manacher's Algorithm to find the longest palindromic substring string preprocess(const string& s) { if (s.empty()) return "^$"; string ret = "^"; for (char c : s) { ret += "#" + string(1, c); } ret += "#$"; return ret; } void manachersAlgorithm(const string& inputStr) { string str = preprocess(inputStr); // Preprocess the original string by adding '#' between characters. int n = str.length(); vector<int> P(n, 0); // Array to store palindrome radius at each position. int center = 0; // Center of the current palindrome being expanded. int mirror = 0; // Mirror index of `i` around `center`. int rightBoundary = 0;// Right boundary of the current palindrome. for (int i = 1; i < n - 1; ++i) { mirror = 2 * center - i; // Calculate mirrored index about the current center. if (rightBoundary > i) { // If within the bounds of a known palindrome, P[i] = min(rightBoundary - i, P[mirror]); // Use symmetry property and avoid unnecessary comparisons. } // Attempt to expand palindrome centered at `i`. while (str[i + 1 + P[i]] == str[i - 1 - P[i]]) { P[i]++; } // Update the center and right boundary if necessary. if (i + P[i] > rightBoundary) { center = i; rightBoundary = i + P[i]; } } // Find the maximum element in P array which corresponds to the longest palindrome. int maxLen = 0; int centerIndex = 0; for (int i = 1; i < n - 1; ++i) { if (P[i] > maxLen) { maxLen = P[i]; centerIndex = i; } } cout << "Longest Palindrome Substring: "; for (int j = centerIndex - maxLen; j <= centerIndex + maxLen; ++j) { if (str[j] != '#') cout << str[j]; // Ignore added special character '#'. } cout << endl; } int main() { string inputStr; cout << "Enter the string: "; cin >> inputStr; manachersAlgorithm(inputStr); return 0; } ``` #### 关键点解析 1. **字符串预处理** 原始字符串通过在字符间插入特殊字符(如 `#`),将其转换成一个新的字符串形式,这样无论原始字符串是奇数长度还是偶数长度,最终都会变成奇数长度。 2. **中心扩展优化** 使用镜像对称性质减少不必要的计算量,在某些情况下可以直接利用之前已经计算好的结果来加速当前位置的回文半径计算过程[^5]。 3. **边界条件处理** 当前索引超出右边界时,则需要重新从头开始逐个匹配字符以找到新的有效范围;如果未越界则可借助已有数据快速得出结论。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值