找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串

找到最大回文子串

Problem statement:

问题陈述:

Given a string, you have to find the largest palindromic substring using O(1) space complexity.

给定一个字符串,您必须使用O(1)空间复杂度找到最大的回文子字符串。

    Input:
    T Test case
    T no of input string will be given to you.

    E.g.
    3
    
    abcsouuoshgcba
    includeaedulcin
    aaaaa
    
    Constrain 
    1≤ length (string) ≤100
    
    Output:
    Print the largest palindromic substring form the given string.

Example

    T=3

    Input:
    abcsouuoshgcba
    
    Output:
    souuos
    
    Input:
    includeaedulcin
    
    Output:
    cludeaedulc
    
    Input:
    aaaaa
    
    Output:
    aaaaa

Explanation with example:

举例说明:

Let there is a string str.

让我们有一个字符串str 。

Now possible arrangements are:

现在可能的安排是:

  1. Single middle characters like aba

    像aba这样的单个中间字符

  2. Paired middle characters like bb

    配对的中间字符,如bb

To find the largest palindromic substring we follow these steps,

要找到最大的回文子串,请按照以下步骤操作,

  1. We start with the first index and go to the end of the string.

    我们从第一个索引开始,然后到字符串的末尾。

  2. Every time we take two variables

    每次我们取两个变量

  3. For the first possible arrangement, we initialize the first variable with the previous index of the current index and initialize the second variable with the next index of the current index.

    对于第一种可能的安排,我们使用当前索引的上一个索引初始化第一个变量,并使用当前索引的下一个索引初始化第二个变量。

  4. For the second possible arrangement, we initialize the first variable with the current index and initialize the second variable with the next variable of the current index.

    对于第二种可能的安排,我们用当前索引初始化第一个变量,并用当前索引的下一个变量初始化第二个变量。

  5. If the character at the first variable place is equal with the character at the second variable place then every time we decrease the first index by one and increase the second index by one and continue the process until or unless the first variable value will be greater than or equals to zero and the second variable value will be less than the length of the string.

    如果第一个变量位置的字符与第二个变量位置的字符相等,则每次我们将第一个索引减小一个,然后将第二个索引增大一个,然后继续执行该过程,直到或除非第一个变量值大于或等于零,并且第二个变量值将小于字符串的长度。

  6. Every time we will add up two with the length of the palindrome substring count.

    每次我们将用回文子串计数的长度加起来两个。

  7. If the character at both the variable place is not the same then we compare with the palindrome substring length.

    如果两个变量位置的字符都不相同,则将其与回文子串的长度进行比较。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

string count_palindrom(string str)
{
    int len = str.length();
    int max_length = 0;
    int start = 0;
    for (int i = 0; i < len; i++) {
        int j = i - 1;
        int k = i + 1;
        int count = 1;
        while (j >= 0 && k < len) {
            if (str[j] == str[k]) {
                count += 2;
                if (max_length < count) {
                    max_length = count;
                    start = j;
                }
                j--;
                k++;
            }
            else {
                break;
            }
        }
        j = i;
        k = i + 1;
        count = 0;
        while (j >= 0 && k < len) {
            if (str[j] != str[k])
                break;
            else {
                count += 2;
                if (max_length < count) {
                    max_length = count;
                    start = j;
                }
                j--;
                k++;
            }
        }
    }
    string s = "";
    if (start == 0 && max_length == 0) {
        return s + str[0];
    }
    for (int i = 0; i < max_length; i++) {
        s += str[start + i];
    }
    return s;
}

int main()
{
    //code
    int t;
    cout << "Test Case : ";
    cin >> t;
    while (t--) {
        string str;
        cout << "Enter the string : ";
        cin >> str;
        cout << "The palindromic substrings is : " << count_palindrom(str) << endl;
    }
    return 0;
}

Output

输出量

Test Case : 3
Enter the string : abcsouuoshgcba
The palindromic substrings is : souuos
Enter the string : includeaedulcin
The palindromic substrings is : cludeaedulc
Enter the string : aaaaa
The palindromic substrings is : aaaaa


翻译自: https://www.includehelp.com/icp/find-the-largest-palindromic-substring-using-o-1-space-complexity.aspx

找到最大回文子串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值