Skip to content

Commit

Permalink
[2000][1A] CF629C DP
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Jul 9, 2021
1 parent c425c89 commit 0f5bfa4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
57 changes: 57 additions & 0 deletions main/600-699/629C.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"bufio"
. "fmt"
"io"
)

// github.com/EndlessCheng/codeforces-go
func CF629C(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
const mod = 1e9 + 7

var n, m, sum, min int
var s []byte
Fscan(in, &n, &m, &s)
for _, b := range s {
if b == '(' {
sum++
} else {
if sum--; sum < min {
min = sum
}
}
}
d := n - m
if d+min < 0 {
Fprint(out, 0)
return
}

dp := make([][]int, d+1)
for i := range dp {
dp[i] = make([]int, d+1)
}
dp[0][0] = 1
for i := 1; i <= d; i++ {
for j := 0; j <= d; j++ {
if j > 0 {
dp[i][j] = dp[i-1][j-1]
}
if j < d {
dp[i][j] = (dp[i][j] + dp[i-1][j+1]) % mod
}
}
}

ans := int64(0)
for i, dv := range dp {
for j := -min; j <= d && j+sum <= d; j++ {
ans = (ans + int64(dv[j])*int64(dp[d-i][j+sum])) % mod
}
}
Fprint(out, ans)
}

//func main() { CF629C(os.Stdin, os.Stdout) }
29 changes: 29 additions & 0 deletions main/600-699/629C_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/EndlessCheng/codeforces-go/main/testutil"
"testing"
)

// https://codeforces.com/problemset/problem/629/C
// https://codeforces.com/problemset/status/629/problem/C
func TestCF629C(t *testing.T) {
// just copy from website
rawText := `
inputCopy
4 1
(
outputCopy
4
inputCopy
4 4
(())
outputCopy
1
inputCopy
4 3
(((
outputCopy
0`
testutil.AssertEqualCase(t, rawText, 0, CF629C)
}

0 comments on commit 0f5bfa4

Please sign in to comment.