CF149D.Coloring Brackets
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.
You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not.
In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one.

You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:
- Each bracket is either not colored any color, or is colored red, or is colored blue.
- For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored.
- No two neighboring colored brackets have the same color.
Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109 + 7).
曾经,Petya 读到一道关于括号序列的问题。他深入思考了很久,却未能找到解法。今天,你将面对它。
给定一个字符串 s,它表示一个合法的括号序列。所谓合法的括号序列,是指由左括号 "(" 和右括号 ")" 构成的序列,使得在其中插入数字与运算符后,能够形成一个合法的数学表达式。例如,"(())()" 和 "()" 是合法的括号序列;而 ")()" 和 "((" 则不是。
在合法的括号序列中,每个括号都对应一个匹配的括号(即每个左括号对应一个匹配的右括号,反之亦然)。例如,在下图所示的括号序列中,第三个括号与第六个括号匹配,第五个括号与第四个括号匹配。

你可以对括号序列中的某些括号进行染色,要求满足以下三个条件:
- 每个括号要么不染色,要么染成红色,要么染成蓝色;
- 对于任意一对匹配的括号,恰好有一个被染色。换言之,对任意一个括号,它自身或其匹配括号中有且仅有一个被染色;
- 任意两个相邻的已染色括号不能染成同一种颜色。
求满足上述条件的染色方案总数。若两种染色方案在至少一个括号的颜色上不同,则视为不同方案。由于答案可能非常大,请输出其对 1000000007(即 109+7)取模的结果。
输入格式
The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.
第一行包含一个字符串 s(2 ≤ ∣s∣ ≤ 700),表示一个合法的括号序列。
输出格式
Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7).
输出唯一的数字——满足上述条件的括号序列染色方案数对 1000000007(即 109+7)取模的结果。
输入输出样例
输入#1
(())
输出#1
12
输入#2
(()())
输出#2
40
输入#3
()
输出#3
4
说明/提示
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.

The two ways of coloring shown below are incorrect.

我们来考虑第一个样例测试。样例中的括号序列可以被染色,例如如下两幅图所示。

下面所示的两种染色方式是错误的。

输入解题思路,AI测评打分。不知道怎么写?