#P3059. [USACO12NOV] Concurrently Balanced Strings G
[USACO12NOV] Concurrently Balanced Strings G
Description
Farmer John raises a very special breed of cows known for their distinctive appearance, where each cow has a huge circular spot on its hide that, depending on the cow’s orientation, may look like a left parenthesis or a right parenthesis.
One morning, John arranges his cows into rows, each with cows (). Since the cows face arbitrary directions, this formation can be described by parenthesis strings of length . John is excited to notice that his herd has some “concurrently balanced” ranges, where the range is concurrently balanced only if in each string the substring over that range is balanced (we will define the meaning of a single parenthesis string being balanced below). For example, if , we have
Then the range is concurrently balanced because , , and . The ranges and are also concurrently balanced.
Given parenthesis strings of length , help John count the number of pairs such that the range is concurrently balanced across all strings.
There are several equivalent ways to define “balanced” for a single parenthesis string. Perhaps the simplest is that the total numbers of parentheses must be equal, and for any prefix of the string, the number of left parentheses must be at least as many as the number of right parentheses. For example, the following strings are balanced:
And these strings are not balanced:
Given sequences of length consisting of parentheses, how many intervals are there such that the corresponding substrings are balanced in all sequences?
Input Format
The first line contains two integers and .
The second through the -th lines: each line contains a parenthesis string of length .
Output Format
The first line contains a single integer, the number of concurrently balanced ranges.
3 14
)()((())))(())
()(()()()((())
)))(()()))(())
3
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号