Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 有效括号的最长长度问题_String - Fatal编程技术网

String 有效括号的最长长度问题

String 有效括号的最长长度问题,string,String,我陷入了查找包含“(”或“)”的最长有效括号子字符串的长度的问题。实际上,有很多方法可以解决这个问题,但我试图找到两个字符串中最长的公共子字符串(LCS) 我得到运行时错误 第1061行:字符9:运行时错误:向0x7ffd2d443260添加无符号偏移量溢出到0x7ffd2d44325f(basic_string.h) 摘要:UndefinedBehaviorSanitizer:undefined behavior/usr/bin/./lib/gcc/x86_64-linux-gnu/8/../

我陷入了查找包含“(”或“)”的最长有效括号子字符串的长度的问题。实际上,有很多方法可以解决这个问题,但我试图找到两个字符串中最长的公共子字符串(LCS)

我得到运行时错误

第1061行:字符9:运行时错误:向0x7ffd2d443260添加无符号偏移量溢出到0x7ffd2d44325f(basic_string.h)
摘要:UndefinedBehaviorSanitizer:undefined behavior/usr/bin/./lib/gcc/x86_64-linux-gnu/8/../../../../../../include/c++/8/bits/basic_string.h:1070:9

请检查这有什么问题

class Solution {
public:
    int LCS(string s1,string s2)
    {
        int m = s1.length();
        int n = s2.length();

        int dp[m+1][n+1];
        memset(dp,0,sizeof(dp));
        for(int i=0;i<=m;i++)
        {
            for(int j=0;j<=n;j++)
                dp[i][j] = 0;
        }
        int res = 0;
        for(int i=0;i<=m;i++)
        {
            for(int j=0;j<=n;j++)
            {
                if(i==j)
                    dp[i][j] = 0;
                else if(s1[i-1]==s2[j-1]){
                    dp[i][j] = 1 + dp[i-1][j-1];
                    res = max(res,dp[i][j]);
                }
                else
                    dp[i][j] = 0;
            }
        }
        return res;
    }
    int longestValidParentheses(string s) {
        // here we can find the longest LCS..
        if(s.length()==0 || s.length()==1)
            return 0;
         string str = "";
    for(int i=s.length()-1;i>=0;i--)
    {
        if(s[i]==')')
        str += '(';
        else if(s[i]=='(')
        str += ')';
    }

        int res = LCS(s,str);
        return res;
    }
};
类解决方案{
公众:
int LCS(字符串s1、字符串s2)
{
int m=s1.length();
int n=s2.length();
int-dp[m+1][n+1];
memset(dp,0,sizeof(dp));

对于(int i=0;i)您可以共享原始问题陈述的链接以获取更多信息吗?谢谢!您可以共享原始问题陈述的链接以获取更多信息吗?谢谢!