📌  相关文章
📜  使用动态规划的给定字符串的不同回文子字符串

📅  最后修改于: 2021-09-17 07:18:57             🧑  作者: Mango

给定一个由小写字母组成的字符串str ,任务是找到给定字符串 的所有不同回文子字符串。

例子:

方法:这里已经使用 Manacher 算法讨论了这个问题的解决方案。然而,我们也可以使用动态规划来解决它。
创建一个数组dp[][] ,其中dp[i][j]设置为1如果str[i…j]是回文,否则为0 。数组生成后,将所有回文子串存储在映射中,以获得不同子串的计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of distinct palindromic sub-strings
// of the given string s
int palindromeSubStrs(string s)
{
 
    // To store the positions of
    // palindromic sub-strings
    int dp[s.size()][s.size()];
    int st, end, i, j, len;
 
    // Map to store the sub-strings
    map m;
    for (i = 0; i < s.size(); i++) {
 
        // Sub-strings of length 1 are palindromes
        dp[i][i] = 1;
 
        // Store continuous palindromic sub-strings
        m[string(s.begin() + i, s.begin() + i + 1)] = 1;
    }
 
    // Store palindromes of size 2
    for (i = 0; i < s.size() - 1; i++) {
        if (s[i] == s[i + 1]) {
            dp[i][i + 1] = 1;
            m[string(s.begin() + i, s.begin() + i + 2)] = 1;
        }
 
        // If str[i...(i+1)] is not a palindromic
        // then set dp[i][i + 1] = 0
        else {
            dp[i][i + 1] = 0;
        }
    }
 
    // Find palindromic sub-strings of length>=3
    for (len = 3; len <= s.size(); len++) {
        for (st = 0; st <= s.size() - len; st++) {
 
            // End of palindromic substring
            end = st + len - 1;
 
            // If s[start] == s[end] and
            // dp[start+1][end-1] is already palindrome
            // then s[start....end] is also a palindrome
            if (s[st] == s[end] && dp[st + 1][end - 1]) {
 
                // Set dp[start][end] = 1
                dp[st][end] = 1;
                m[string(s.begin() + st, s.begin() + end + 1)] = 1;
            }
 
            // Not a palindrome
            else
                dp[st][end] = 0;
        }
    }
 
    // Return the count of distinct palindromes
    return m.size();
}
 
// Driver code
int main()
{
    string s = "abaaa";
    cout << palindromeSubStrs(s);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.HashMap;
 
class GFG
{
 
    // Function to return the count
    // of distinct palindromic sub-strings
    // of the given string s
    static int palindromeSubStrs(String s)
    {
 
        // To store the positions of
        // palindromic sub-strings
        int[][] dp = new int[s.length()][s.length()];
        int st, end, i, len;
 
        // Map to store the sub-strings
        HashMap m = new HashMap<>();
 
        for (i = 0; i < s.length(); i++)
        {
 
            // Sub-strings of length 1 are palindromes
            dp[i][i] = 1;
 
            // Store continuous palindromic sub-strings
            m.put(s.substring(i, i + 1), true);
        }
 
        // Store palindromes of size 2
        for (i = 0; i < s.length() - 1; i++)
        {
            if (s.charAt(i) == s.charAt(i + 1))
            {
                dp[i][i + 1] = 1;
                m.put(s.substring(i, i + 2), true);
            }
 
            // If str[i...(i+1)] is not a palindromic
            // then set dp[i][i + 1] = 0
            else
                dp[i][i + 1] = 0;
        }
 
        // Find palindromic sub-strings of length>=3
        for (len = 3; len <= s.length(); len++)
        {
            for (st = 0; st <= s.length() - len; st++)
            {
 
                // End of palindromic substring
                end = st + len - 1;
 
                // If s[start] == s[end] and
                // dp[start+1][end-1] is already palindrome
                // then s[start....end] is also a palindrome
                if (s.charAt(st) == s.charAt(end) &&
                    dp[st + 1][end - 1] == 1)
                {
 
                    // Set dp[start][end] = 1
                    dp[st][end] = 1;
                    m.put(s.substring(st, end + 1), true);
                }
 
                // Not a palindrome
                else
                    dp[st][end] = 0;
            }
        }
 
        // Return the count of distinct palindromes
        return m.size();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "abaaa";
        System.out.println(palindromeSubStrs(s));
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of the approach
 
# import numpy lib as np
import numpy as np;
 
# Function to return the count
# of distinct palindromic sub-strings
# of the given string s
def palindromeSubStrs(s) :
 
    # To store the positions of
    # palindromic sub-strings
    dp = np.zeros((len(s),len(s)));
     
    # Map to store the sub-strings
    m = {};
     
    for i in range(len(s)) :
 
        # Sub-strings of length 1 are palindromes
        dp[i][i] = 1;
 
        # Store continuous palindromic sub-strings
        m[s[i: i + 1]] = 1;
     
 
    # Store palindromes of size 2
    for i in range(len(s)- 1) :
        if (s[i] == s[i + 1]) :
            dp[i][i + 1] = 1;
            m[ s[i : i + 2]] = 1;
          
 
        # If str[i...(i+1)] is not a palindromic
        # then set dp[i][i + 1] = 0
        else :
            dp[i][i + 1] = 0;
 
    # Find palindromic sub-strings of length>=3
    for length in range(3,len(s) + 1) :
        for st in range(len(s) - length + 1) :
 
            # End of palindromic substring
            end = st + length - 1;
 
            # If s[start] == s[end] and
            # dp[start+1][end-1] is already palindrome
            # then s[start....end] is also a palindrome
            if (s[st] == s[end] and dp[st + 1][end - 1]) :
 
                # Set dp[start][end] = 1
                dp[st][end] = 1;
                m[s[st : end + 1]] = 1;
 
            # Not a palindrome
            else :
                dp[st][end] = 0;
 
    # Return the count of distinct palindromes
    return len(m);
 
 
# Driver code
if __name__ == "__main__" :
 
    s = "abaaa";
    print(palindromeSubStrs(s));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to return the count
    // of distinct palindromic sub-strings
    // of the given string s
    static int palindromeSubStrs(String s)
    {
 
        // To store the positions of
        // palindromic sub-strings
        int[,] dp = new int[s.Length, s.Length];
        int st, end, i, len;
 
        // Map to store the sub-strings
        Dictionary m = new Dictionary();
 
        for (i = 0; i < s.Length; i++)
        {
 
            // Sub-strings of length 1 are palindromes
            dp[i,i] = 1;
 
            // Store continuous palindromic sub-strings
            if(!m.ContainsKey(s.Substring(i, 1)))
                m.Add(s.Substring(i, 1), true);
        }
 
        // Store palindromes of size 2
        for (i = 0; i < s.Length - 1; i++)
        {
            if (s[i] == s[i + 1])
            {
                dp[i, i + 1] = 1;
                if(!m.ContainsKey(s.Substring(i, 2)))
                    m.Add(s.Substring(i, 2), true);
            }
 
            // If str[i...(i+1)] is not a palindromic
            // then set dp[i,i + 1] = 0
            else
                dp[i, i + 1] = 0;
        }
 
        // Find palindromic sub-strings of length>=3
        for (len = 3; len <= s.Length; len++)
        {
            for (st = 0; st <= s.Length - len; st++)
            {
 
                // End of palindromic substring
                end = st + len - 1;
 
                // If s[start] == s[end] and
                // dp[start+1,end-1] is already palindrome
                // then s[start....end] is also a palindrome
                if (s[st] == s[end] &&
                    dp[st + 1, end - 1] == 1)
                {
 
                    // Set dp[start,end] = 1
                    dp[st, end] = 1;
                    m.Add(s.Substring(st, end + 1-st), true);
                }
 
                // Not a palindrome
                else
                    dp[st, end] = 0;
            }
        }
 
        // Return the count of distinct palindromes
        return m.Count;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String s = "abaaa";
        Console.WriteLine(palindromeSubStrs(s));
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程