C++ 无法找出我的代码在这个括号序列问题中得到错误答案的测试用例

C++ 无法找出我的代码在这个括号序列问题中得到错误答案的测试用例,c++,dynamic-programming,parentheses,C++,Dynamic Programming,Parentheses,您将获得一个括号序列S。要求您在S中插入尽可能少的括号,以便生成的序列T匹配良好 你能告诉我你能得到多少不同的T 假设S=()),您可以得到(())或()() 一行包含序列S 对于30%的数据,1≤ |S|≤ 10 对于60%的数据,1≤ |S|≤ 200 对于100%的数据,1≤ |S|≤ 1000 输出2个整数,指示需要插入S的最小括号数和不同T的数目。不同的T的数量可能非常大,因此以10^9+7的模输出答案 解决这个问题的一种方法是将原始序列分成两部分。一个只需插入(,另一个只需插入)

您将获得一个括号序列
S
。要求您在
S
中插入尽可能少的括号,以便生成的序列
T
匹配良好

你能告诉我你能得到多少不同的
T

假设
S=())
,您可以得到
(())
()(

一行包含序列
S

  • 对于30%的数据,
    1≤ |S|≤ 10
  • 对于60%的数据,
    1≤ |S|≤ 200
  • 对于100%的数据,
    1≤ |S|≤ 1000
输出2个整数,指示需要插入
S
的最小括号数和不同
T
的数目。不同的
T
的数量可能非常大,因此以
10^9+7
的模输出答案

解决这个问题的一种方法是将原始序列分成两部分。一个只需插入
,另一个只需插入
。这两部分可以用同样的方法解决

考虑一个序列
(()(()
),为了避免重复,我们只在
)前面或序列的最后一个位置插入
。可以在每个位置插入的
的数量是有限的,让它成为
限制[i]

假设在第一个
i
位置插入
f[i][j]
,然后将
f[i][j]=f[i-1][0]+f[i-1][1]+。。。f[i-1][min(j,极限[i-1])]

然而,当我提交代码时,我得到了60/100 WA。我无法测试导致错误答案的案例,因此我不知道如何改进代码

以下是我提交的代码:

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

const int modulo = pow(10, 9) + 7;

int split(string);
void inverse(string&);
void getInsLimit(vector<int>&, string, int&);
int getNumOfWays(vector<int>);

int record[1001][1001]; // record nums of ways to insert j parenthesies into the first i position

int main() {

    string input;
    while (getline(cin, input)) {
        int LRpos = split(input); // return the pos after the last ele of L 
        string L = input.substr(0, LRpos), R = input.substr(LRpos, input.size() - LRpos);
        inverse(L);

        int insCnt = 0, wayCnt = 0, tmpL = 0, tmpR = 0;


        vector<int> insLimit;
        getInsLimit(insLimit, L, insCnt);
        tmpL = getNumOfWays(insLimit);


        getInsLimit(insLimit, R, insCnt);
        tmpR = getNumOfWays(insLimit);

        if (tmpL == 0 || tmpR == 0) {
            wayCnt = tmpL + tmpR;
        }
        else {
            for (int i = 0; i < tmpL; ++i) {
                wayCnt += tmpR;
                if (wayCnt >= modulo) {
                    wayCnt -= modulo;
                }
            }

        }

        cout << insCnt << ' ' << wayCnt << endl;

    }

    return 0;
}

int split(string input) {
    int pos = 0;
    int lcnt = 0;
    for (string::size_type i = 0; i < input.size(); ++i) {
        if (input[i] == '(') {
            ++lcnt;
        }else {
            --lcnt;
        }
        if (lcnt < 0) { // less than 0 means it needs '('
            lcnt = 0;
            pos = i + 1;
        }
    }
    return pos;
}

void inverse(string &str) {
    for (string::size_type i = 0; i < str.size(); ++i) {
        str[i] = str[i] == '(' ? ')':'(';
    }
}

void getInsLimit(vector<int>& insLimit, string str, int& insCnt) {
    insLimit.clear();

    if (str.empty())
        return;

    int lCnt = 0;
    for (string::size_type i = 0; i < str.size(); ++i) {
        if (str[i] == '(') {
            ++lCnt;
        }else {
            --lCnt;
        }
        if (lCnt > 0 && (i == str.size() - 1 || str[i + 1] == '(')) {
            insLimit.push_back(lCnt);
        } 
        if (lCnt == 0) { // means that it needn't parenthesies at all
            insLimit.clear();
        }
    }

    insCnt += lCnt;
}

int getNumOfWays(vector<int> insLimit) {

    if (insLimit.empty()) {
        return 0; 
    }

    int maxI = insLimit.size() - 1, maxJ = insLimit[maxI];

    memset(record, 0, 1001 * 1001);

    // Initialize the first line
    for (int j = 0; j <= insLimit[0]; ++j) { // can be equal
        record[0][j] = insLimit[0]; // actually it can only be 1
    }

    for (int i = 1; i <= maxI; ++i) {
        for (int j = 0; j <= insLimit[i]; ++j) { // after limit, record[i][j] = 0, equals initial value
            if (j == 0)
                record[i][j] = 1;
            else {
                record[i][j] = record[i - 1][j] + record[i][j - 1];
                if (record[i][j] >= modulo)
                    record[i][j] -= modulo;
            }
        }
    }

    for (int j = 0; j < maxJ; ++j) {
        record[maxI][j] = 0;
    } // logically should be 0, unnecessary in this problem

    return record[maxI][maxJ];
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int模=pow(10,9)+7;
整数分割(字符串);
空逆(字符串&);
void getInsLimit(向量&,字符串,整数&);
int getNumOfWays(向量);
int记录[1001][1001];//记录将j括号插入第一个i位置的方法数
int main(){
字符串输入;
while(getline(cin,输入)){
int LRpos=split(input);//返回L的最后一个元素之后的位置
字符串L=input.substr(0,LRpos),R=input.substr(LRpos,input.size()-LRpos);
逆(L);
int insCnt=0,wayCnt=0,tmpL=0,tmpR=0;
向量极限;
getInsLimit(insLimit,L,insCnt);
tmpL=getNumOfWays(insLimit);
getInsLimit(insLimit,R,insCnt);
tmpR=getNumOfWays(insLimit);
如果(tmpL==0 | | tmpR==0){
wayCnt=tmpL+tmpR;
}
否则{
对于(int i=0;i=模){
wayCnt-=模;
}
}
}
库特