📌  相关文章
📜  查询子字符串[L…R]是否为回文

📅  最后修改于: 2021-04-29 15:32:10             🧑  作者: Mango

给定一个字符串strQ查询。每个查询由两个数字LR组成。任务是打印子字符串[L…R]是否为回文。

例子:

简单方法:天真的方法是检查每个子字符串是否回文。在最坏的情况下,每个查询最多可能占用O(Q)

高效的方法:一种有效的方法是使用动态编程来解决上述问题。我们最初可以创建DP表,该表存储substring [i….j]是否为回文。我们维护一个以自下而上的方式填充的布尔值dp [n] [n] 。如果子字符串是回文,则dp [i] [j]的值为true,否则为false。为了计算dp [i] [j],我们首先检查dp [i + 1] [j-1]的值,如果该值为true且s [i]s [j]相同,则使dp [i] [j]是。否则,将dp [i] [j]的值设为false。

现在,对于每个查询,请检查dp [l] [r]是否为true。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 100
  
// Pre-processing function
void pre_process(bool dp[N][N], string s)
{
  
    // Get the size of the string
    int n = s.size();
  
    // Initially mark every
    // position as false
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            dp[i][j] = false;
    }
  
    // For the length
    for (int j = 1; j <= n; j++) {
  
        // Iterate for every index with
        // length j
        for (int i = 0; i <= n - j; i++) {
  
            // If the length is less than 2
            if (j <= 2) {
  
                // If characters are equal
                if (s[i] == s[i + j - 1])
                    dp[i][i + j - 1] = true;
            }
  
            // Check for equal
            else if (s[i] == s[i + j - 1])
                dp[i][i + j - 1] = dp[i + 1][i + j - 2];
        }
    }
}
  
// Function to answer every query in O(1)
void answerQuery(int l, int r, bool dp[N][N])
{
    if (dp[l][r])
        cout << "Yes\n";
    else
        cout << "No\n";
}
  
// Driver code
int main()
{
    string s = "abaaab";
    bool dp[N][N];
    pre_process(dp, s);
  
    int queries[][2] = { { 0, 1 }, { 1, 5 } };
    int q = sizeof(queries) / sizeof(queries[0]);
  
    for (int i = 0; i < q; i++)
        answerQuery(queries[i][0], queries[i][1], dp);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    static int N = 100;
  
    // Pre-processing function
    static void pre_process(boolean dp[][], char[] s)
    {
  
        // Get the size of the string
        int n = s.length;
  
        // Initially mark every
        // position as false
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = false;
            }
        }
  
        // For the length
        for (int j = 1; j <= n; j++) {
  
            // Iterate for every index with
            // length j
            for (int i = 0; i <= n - j; i++) {
  
                // If the length is less than 2
                if (j <= 2) {
  
                    // If characters are equal
                    if (s[i] == s[i + j - 1]) {
                        dp[i][i + j - 1] = true;
                    }
                }
  
                // Check for equal
                else if (s[i] == s[i + j - 1]) {
                    dp[i][i + j - 1] = dp[i + 1][i + j - 2];
                }
            }
        }
    }
  
    // Function to answer every query in O(1)
    static void answerQuery(int l, int r, boolean dp[][])
    {
        if (dp[l][r]) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s = "abaaab";
        boolean[][] dp = new boolean[N][N];
        pre_process(dp, s.toCharArray());
  
        int queries[][] = { { 0, 1 }, { 1, 5 } };
        int q = queries.length;
  
        for (int i = 0; i < q; i++) {
            answerQuery(queries[i][0], queries[i][1], dp);
        }
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
N = 100
  
# Pre-processing function
def pre_process(dp, s):
  
    # Get the size of the string
    n = len(s)
  
    # Initially mark every
    # position as false
    for i in range(n):
        for j in range(n):
            dp[i][j] = False
  
    # For the length
    for j in range(1, n + 1):
  
        # Iterate for every index with
        # length j
        for i in range(n - j + 1):
  
            # If the length is less than 2
            if (j <= 2):
  
                # If characters are equal
                if (s[i] == s[i + j - 1]):
                    dp[i][i + j - 1] = True
  
            # Check for equal
            elif (s[i] == s[i + j - 1]):
                dp[i][i + j - 1] = dp[i + 1][i + j - 2]
  
# Function to answer every query in O(1)
def answerQuery(l, r, dp):
  
    if (dp[l][r]):
        print("Yes")
    else:
        print("No")
  
# Driver code
s = "abaaab"
dp = [[0 for i in range(N)]
         for i in range(N)]
pre_process(dp, s)
  
queries = [[0, 1], [1, 5]]
q = len(queries)
  
for i in range(q):
    answerQuery(queries[i][0], 
                queries[i][1], dp)
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
class GFG {
  
    static int N = 100;
  
    // Pre-processing function
    static void pre_process(bool[, ] dp, char[] s)
    {
  
        // Get the size of the string
        int n = s.Length;
  
        // Initially mark every
        // position as false
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i, j] = false;
            }
        }
  
        // For the length
        for (int j = 1; j <= n; j++) {
  
            // Iterate for every index with
            // length j
            for (int i = 0; i <= n - j; i++) {
  
                // If the length is less than 2
                if (j <= 2) {
  
                    // If characters are equal
                    if (s[i] == s[i + j - 1]) {
                        dp[i, i + j - 1] = true;
                    }
                }
  
                // Check for equal
                else if (s[i] == s[i + j - 1]) {
                    dp[i, i + j - 1] = dp[i + 1, i + j - 2];
                }
            }
        }
    }
  
    // Function to answer every query in O(1)
    static void answerQuery(int l, int r, bool[, ] dp)
    {
        if (dp[l, r]) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
  
    // Driver code
    public static void Main()
    {
        string s = "abaaab";
        bool[, ] dp = new bool[N, N];
        pre_process(dp, s.ToCharArray());
  
        int[, ] queries = { { 0, 1 }, { 1, 5 } };
        int q = queries.Length;
  
        for (int i = 0; i < q; i++) {
            answerQuery(queries[i, 0], queries[i, 1], dp);
        }
    }
}
  
// This code is contributed by Ankit.


PHP


输出:
No
Yes