洛谷 P1944 最长括号匹配

url: https://www.luogu.com.cn/problem/P1944

思路:
用动态规划的思路来做。用f[i]表示以s[i]结尾的括号匹配字符串,属性是最大值。从第二个字符开始检查,如果当前字符为 )或 ] ,以及距离上一个匹配好的字符串前的一个字符与其相匹配,则更新长度为f[i] = f[i - 1] + 2,同时可以检查前两个字符,如果为 )或者 ] 说明以那个字符结尾的字符串可以与当前的字符串相连。因为如果可以匹配为字符串则会将结果存在f[i] 中,所以实际上不需要匹配,只需要加上 f[i - f[i - 1] - 2] 即可。最后每次更新下最长的字符串长度和下标,因为要输出第一个最长的,所以只有在严格大于时才更新,最后根据长度和下标从原字符串中构造出答案输出即可。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e6 + 10;
int f[N];
char str[N];
int main()
{

    scanf("%s", str + 1);
    cout << str;
    int len = strlen(str + 1);
    int mlen = 0, id = 0;
    for (int i = 2; i <= len; i ++)
    {
        if (str[i - f[i - 1] -1] == '(' && str[i] == ')' || str[i - f[i - 1] - 1] == '[' && str[i] == ']')
        {

            f[i] = f[i - 1] + f[i - f[i - 1] - 2] + 2;
            if (f[i] > mlen)
            {
                mlen = f[i];
                id = i;
            }
        }
    }
    for (int i = id - mlen + 1; i <= id;  i++)
    {
        cout << str[i];
    }
    return 0;
}
添加新评论