Problem list
Log in
#usaco0061 · USACO 2012-2013 November Gold

Concurrently Balanced Strings

Platinum I 2.0 s 256 MB

Farmer John's cows are all of a very peculiar breed known for its distinctive appearance -- each cow is marked with a giant spot on its hide in the shape of a parenthesis (depending on the direction the cow is facing, this could look like either a left or a right parenthesis).

One morning, Farmer John arranges his cows into K lines each of N cows (1 <= K <= 10, 1 <= N <= 50,000). The cows are facing rather arbitrary directions, so this lineup can be described by K length-N strings of parentheses S_1,..., S_k. Farmer John notes with great excitement that some ranges of his cows are "concurrently balanced", where a range i...j of cows is concurrently balanced only if each of the strings S_1,..., S_k is balanced in that range.

Given K length-N strings of parentheses, help Farmer John count the number of pairs (i,j) such that the range i...j is concurrently balanced. A string of parentheses is balanced if it has equal numbers of ('s and )'s and every prefix has at least as many ('s as )'s.

Input

  • Line 1: Two integers, K and N.
  • Lines 2..K+1: Each line contains a length-N string of parentheses.

Output

A single integer, the number of concurrently balanced ranges.

Example 1

Input
3 14
)()((())))(())
()(()()()((())
)))(()()))(())
Output
3
Source
USACO 2012-2013 November Gold
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code