QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243592#1769. 括号序列LYT0122100 ✓185ms18200kbC++141.7kb2023-11-08 14:43:422023-11-08 14:43:42

Judging History

你现在查看的是最新测评结果

  • [2023-11-08 14:43:42]
  • 评测
  • 测评结果:100
  • 用时:185ms
  • 内存:18200kb
  • [2023-11-08 14:43:42]
  • 提交

answer

#include <iostream>
using namespace std;
const int N=520,mod=1e9+7;
int n,k;
long long int dp[N][N][7];
char s[N];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) cin>>s[i];
    //区间dp,dp[l][r][1/2/3/4/5]
    //1表示 每一位全是'*' 这样的括号序列
    //2表示 左端为'('且右端为')'且这两个括号相匹配 这样的括号序列
    //3表示 左端为'('且右端为'*' 这样的括号序列
    //4表示 左端为'('且右端为')' 这样的括号序列
    //5表示 左端为'*'且右端为')' 这样的括号序列
    //6表示 左端为'*'且右端为'*' 这样的括号序列
    for(int i=1;i<=n;i++) dp[i][i-1][1]=1;
    for(int len=1;len<=n;len++)
    {
        for(int l=1,r=l+len-1;r<=n;l++,r++)
        {
            if(len<=k)
            {
                if((s[r]=='*' || s[r]=='?') && dp[l][r-1][1]==1) dp[l][r][1]=1;
                else dp[l][r][1]=0;
            }
            if(len>=2)
            {
                if((s[l]=='(' || s[l]=='?') && (s[r]==')' || s[r]=='?'))
                    dp[l][r][2]=(dp[l+1][r-1][1]+dp[l+1][r-1][3]+dp[l+1][r-1][4]+dp[l+1][r-1][5])%mod;
                for(int i=l;i<=r-1;i++)
                {
                    dp[l][r][3]=(dp[l][r][3]+dp[l][i][4]*dp[i+1][r][1])%mod;
                    dp[l][r][4]=(dp[l][r][4]+(dp[l][i][3]+dp[l][i][4])*dp[i+1][r][2])%mod;
                    dp[l][r][5]=(dp[l][r][5]+(dp[l][i][5]+dp[l][i][6])*dp[i+1][r][2])%mod;
                    dp[l][r][6]=(dp[l][r][6]+dp[l][i][5]*dp[i+1][r][1])%mod;
                }
            }
            dp[l][r][4]=(dp[l][r][4]+dp[l][r][2])%mod;
            dp[l][r][6]=(dp[l][r][6]+dp[l][r][1])%mod;
        }
    }
    printf("%lld",dp[1][n][4]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 1ms
memory: 3584kb

input:

14 3
???*?(*????)??

output:

600

result:

ok single line: '600'

Test #2:

score: 5
Accepted
time: 1ms
memory: 3728kb

input:

15 7
?()??)???*?????

output:

1443

result:

ok single line: '1443'

Test #3:

score: 5
Accepted
time: 0ms
memory: 3592kb

input:

15 10
??*??????????(?

output:

7801

result:

ok single line: '7801'

Test #4:

score: 5
Accepted
time: 1ms
memory: 3720kb

input:

38 6
?(????(??*??????*????*????(?(*(??*????

output:

804755392

result:

ok single line: '804755392'

Test #5:

score: 5
Accepted
time: 1ms
memory: 5624kb

input:

39 17
?????*???)????(????*????????????*???)??

output:

734493810

result:

ok single line: '734493810'

Test #6:

score: 5
Accepted
time: 1ms
memory: 3728kb

input:

40 2
?*???(???(?*???))??*????*???*??**(???(??

output:

946388230

result:

ok single line: '946388230'

Test #7:

score: 5
Accepted
time: 1ms
memory: 3984kb

input:

40 11
?????*????????)??(??)?(??????*??????????

output:

513756777

result:

ok single line: '513756777'

Test #8:

score: 5
Accepted
time: 1ms
memory: 5948kb

input:

40 26
????)????????*????????????*?????????()??

output:

156637450

result:

ok single line: '156637450'

Test #9:

score: 5
Accepted
time: 2ms
memory: 7780kb

input:

95 3
???)??????????)?????????*???????????(?(???????????()?????(???????**??????(??*??????????***?????

output:

267516104

result:

ok single line: '267516104'

Test #10:

score: 5
Accepted
time: 2ms
memory: 6032kb

input:

98 22
?*??????*???)??????(??)?)????????*??????????(?????))??????*????????????*****?????***????(???????*?

output:

641483452

result:

ok single line: '641483452'

Test #11:

score: 5
Accepted
time: 2ms
memory: 6220kb

input:

99 15
???*????*??)??)??????(????**????(???*??????*??????*???)????*???????**?????????*????????*?*??????)??

output:

195466063

result:

ok single line: '195466063'

Test #12:

score: 5
Accepted
time: 2ms
memory: 7736kb

input:

100 17
???*????(???????*?????(??(??????*???????*???????*?????)??)?????(??????*??????(????*????*???*)????*??

output:

695865604

result:

ok single line: '695865604'

Test #13:

score: 5
Accepted
time: 2ms
memory: 6220kb

input:

100 33
??(???*????)??)????)????????????*?????**????????????*???????????*????????*?*???????))???????*?????)?

output:

114367997

result:

ok single line: '114367997'

Test #14:

score: 5
Accepted
time: 171ms
memory: 18020kb

input:

497 238
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

211791029

result:

ok single line: '211791029'

Test #15:

score: 5
Accepted
time: 183ms
memory: 18076kb

input:

500 463
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

572342110

result:

ok single line: '572342110'

Test #16:

score: 5
Accepted
time: 170ms
memory: 17624kb

input:

491 88
???????????*?????????????*??????*?????*?????***?????*?????????*?????(??**??????????????*??***????????????????*?????????)*????????????????(*????????????????*?*????????????(??????*???????????*??????????*????*?*????????*???????????*?????*??????????*???????*??????**???????*???*???????????????????...

output:

617614760

result:

ok single line: '617614760'

Test #17:

score: 5
Accepted
time: 173ms
memory: 18088kb

input:

498 135
?*?????????????*?????*??*??????*?????????**?*??*?**??*????*?????????????????????????????????*????**???????????*????*????*????*?????*????????????(()?*????????*???(?*???????????????*????????????*?????????????????????????(??*??(????)???*????)????)*???????????*???????(???????**????????????*?????...

output:

637064892

result:

ok single line: '637064892'

Test #18:

score: 5
Accepted
time: 177ms
memory: 16616kb

input:

500 174
??*????(((*???????????????????????*???*???????*???*?????????????*??*?*??**?????(???(??????????????????*)?????????????(????????*????*????*????????????????????*?????????*??????*???*??????????????*?????????????????*???*???????????????????????????????????*?*????????????????????????????*??*??????...

output:

835939342

result:

ok single line: '835939342'

Test #19:

score: 5
Accepted
time: 185ms
memory: 18200kb

input:

500 221
????????????*?*??*?????????*???*??????*(???)???)????(?*?????(??**??*???*??????????*??*?*???????*??????*???????*?????)????*???)??)??*??????????????????*???*???????????????????*????????**????????*?????????*??????*??*?*??????????????????*?*????????*???*??????????*????????????*???????????????*??...

output:

977731606

result:

ok single line: '977731606'

Test #20:

score: 5
Accepted
time: 185ms
memory: 16596kb

input:

500 316
???*?*?????*???*???(??????????????*????(?????????????????*?????????????????????*???????????*??**??????*????????????????*????)???*????????????*?*????????????????????????*?*??*?????????????????????*?????????**?*???**???*????*?????????????????????????????????????*???????*??????????*?**????????*...

output:

875239106

result:

ok single line: '875239106'

Extra Test:

score: 0
Extra Test Passed