codeforces-149D(区间dp + 递归)

本文探讨了给定正确括号序列的涂色问题,旨在找出所有可能的涂色方案,确保相邻涂色括号颜色不同,且每对括号至少有一方被涂色。通过动态规划方法,实现了高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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).

Input

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

Output

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7).

Examples

Input

(())

Output

12

Input

(()())

Output

40

Input

()

Output

4

Note

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.

 

题意:给出一段匹配好的括号序列,给他们涂颜色,要么不涂色,要么只能涂红,蓝两种颜色,每两个相邻的括号不能涂相同的颜色,一个括号的左右必须有一个涂。求涂色方案数。

参考:https://blog.youkuaiyun.com/loy_184548/article/details/65448189

https://blog.youkuaiyun.com/u013534123/article/details/78095359

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>

using namespace std;

const int maxn = 705;
const int mod = 1e9 + 7;


int match[maxn];
long long dp[maxn][maxn][3][3];//dp[l][r][i][j] 表示l涂i,r涂j。
string str;

void getmatch()
{
    int n = str.size();
    stack <int> st;
    for(int i = 0;i < n;i ++)
    {
        if(str[i] == '(')
            st.push(i);
        else
        {
                match[i] = st.top();
                match[st.top()] = i;
                st.pop();
        }
    }
}

void dfs(int l,int r)
{
    if(l == r - 1)//一对括号
    {
        dp[l][r][0][1] = 1;
        dp[l][r][0][2] = 1;
        dp[l][r][1][0] = 1;
        dp[l][r][2][0] = 1;
        return ;
    }
    if(match[l] == r)//两个端点匹配
    {
        dfs(l + 1,r - 1);
        for(int i = 0;i < 3;i ++)
            for(int j = 0;j < 3;j ++)
        {
            if(j != 1) dp[l][r][0][1] = (dp[l][r][0][1] + dp[l + 1][r - 1][i][j]) % mod;
            if(j != 2) dp[l][r][0][2] = (dp[l][r][0][2] + dp[l + 1][r - 1][i][j]) % mod;
            if(i != 1) dp[l][r][1][0] = (dp[l][r][1][0] + dp[l + 1][r - 1][i][j]) % mod;
            if(i != 2) dp[l][r][2][0] = (dp[l][r][2][0] + dp[l + 1][r - 1][i][j]) % mod;
        }
    }
    else
    {
        int k = match[l];//从中间选取匹配的
        dfs(l,k);
        dfs(k + 1,r);
        for(int i = 0;i < 3;i ++)//l
            for(int j = 0;j < 3;j ++)//r
            for(int q = 0;q < 3;q ++)//k
            for(int p = 0;p < 3;p ++)//k + 1
        {
            if((q == 1 || q == 2) && (q == p))
                continue;
            dp[l][r][i][j] = (dp[l][r][i][j] + (dp[l][k][i][q] * dp[k + 1][r][p][j]) % mod) % mod;
        }
    }
}

int main()
{
    while(cin >> str)
    {
        int n = str.size();
        memset(dp,0,sizeof(dp));
        getmatch();
        dfs(0,n - 1);
        long long ans = 0;
        for(int i = 0;i < 3;i ++)
            for(int j = 0;j < 3;j ++)
            ans = (ans + dp[0][n - 1][i][j]) % mod;
        cout << ans << endl;
    }
    return 0;
}

 

<think>好的,我现在需要处理用户关于Codeforces题型总结和解题技巧的请求。首先,回顾之前的对话历史,用户之前询问了ACM学习路线,我给出了分阶段的学习计划和资源推荐。现在用户转向Codeforces的具体题型和解题技巧,这表明他们可能正在准备竞赛,需要更针对性的策略。 首先,我需要确定用户的需求。用户想要的是Codeforces平台上各类题型的分类总结以及对应的解题思路。他们可能希望了解常见的题型类别,每个类别的特点,以及解决这些题目的常用技巧和算法。此外,用户可能还希望得到一些具体的例题和代码示例,以便更好地理解和应用这些技巧。 接下来,我需要参考用户提供的引用内容。引用[1]中提到了算法基础,包括复杂度分析、递归、分治、贪心、排序、前缀和、差分、二分、三分、倍增等。这些内容在Codeforces题目中确实常见,因此需要将这些算法和技巧与具体的题型对应起来,进行分类归纳。 我需要确保回答结构清晰,分点列出各个题型类别,每个类别下包括题型特点、解题技巧、例题及解析。同时,按照用户之前的要求,使用中文回答,行内数学公式用$...$,独立公式用$$...$$,并正确使用LaTeX语法。此外,需要生成相关问题,并在适当的地方添加引用标识。 在整理题型时,要覆盖Codeforces常见的题目类型,如贪心、动态规划、图论、数论、数据结构、构造题、二分/三分、字符串处理、计算几何等。每个类别需要简要说明其特点,解题时的常用方法,并举出具体的题目例子,最好附上题目编号,方便用户查找练习。 同时,要注意结合用户之前的学习路线,推荐相关的资源,比如《算法竞赛入门经典》中的例题,或者在线平台如Codeforces本身的训练建议。此外,可能需要提到一些实战技巧,比如如何快速识别题型、如何优化代码等。 需要避免的是过于泛泛而谈,要尽量具体,给出实际可操作的技巧。例如,在贪心题中,可以提到邻项交换法或后悔法的具体应用;在动态规划中,强调状态设计和转移方程的优化方法。对于每个技巧,最好能对应一个例题,说明如何应用该技巧解决问题。 另外,考虑到用户可能需要代码示例,可以在适当的位置插入简短的代码片段,如动态规划或二分的典型实现,但需注意代码的准确性和简洁性,避免冗长。 最后,生成的相关问题需要围绕用户可能的后续疑问展开,比如如何提高刷题效率,特定题型的深入分析方法,或者推荐Codeforces上的训练计划等。这些问题应自然延伸当前主题,帮助用户进一步探索。 现在,需要检查是否覆盖了用户的所有需求,是否结构清晰,是否引用了之前的资源(如引用[^1]),并确保所有数学公式的正确格式。同时,确保回答真实可靠,基于常见的竞赛知识和经验,避免错误的信息。</think>### Codeforces题型总结与解题技巧分类指南 #### 一、高频核心题型分类 1. **贪心算法** - **题型特点**:决策序列中局部最优选择可推导全局最优解,常见于区间调度、资源分配问题 - **解题技巧**: - 邻项交换法(证明交换后更优):如$n$个数的最大拼接问题(CF 1385B) - 后悔法(优先队列维护候选集):CF 1526C(药水选择) ```cpp // 后悔法模板 priority_queue<int, vector<int>, greater<int>> pq; for(int x : arr){ sum += x; pq.push(x); while(sum < 0){ sum -= pq.top(); pq.pop(); } } ``` - 时间轴处理:CF 1481C(栅栏涂色时序处理)[^1] 2. **动态规划** - **状态设计模式**: - 线性DP:$dp[i]$表示前$i$个元素的最优解(CF 474D 花朵问题) - 状态压缩DP:用位运算表示集合状态(CF 580D 美食家问题) - 树形DP:结合DFS遍历处理子树关系(CF 1092F 树权值分配) - **优化技巧**: - 斜率优化:维护凸包降低时间复杂度至$O(n)$(CF 319C 木材切割) - 滚动数组:空间复杂度优化至$O(1)$(CF 1114D 颜色填充) 3. **图论算法** - **高频考点**: | 算法类型 | 应用场景 | 例题 | |----------------|-----------------------------------|----------------| | 拓扑排序 | 任务调度/依赖解析 | CF 1385E | | 强连通分量 | 缩点处理环结构 | CF 427C | | 网络流 | 二分图匹配/资源分配 | CF 1139E | | 最短路变形 | 分层图/边权特殊处理 | CF 1473E | 4. **数论问题** - **核心工具**: - 欧拉定理:处理模指数运算(CF 615D 质因子乘积) - 中国剩余定理:同余方程组求解(CF 687B) - 莫比乌斯反演:统计互质对数量(CF 547C) - **特殊技巧**: $$ \sum_{d|n}\mu(d) = \begin{cases} 1 & n=1 \\ 0 & n>1 \end{cases} $$ 用于快速计算GCD相关问题 #### 二、特征题型专项突破 1. **构造题(Constructive Algorithms)** - **识别特征**:输出满足特定条件的结构而非数值结果 - **经典解法**: - 奇偶性构造:CF 1421B(矩阵路径奇偶性) - 平衡分配法:CF 1343D(调整数组使对称元素相等) - 极端值构造:CF 1375E(通过逆序操作排序数组) 2. **交互题(Interactive Problems)** - **解题框架**: ```python low, high = 1, 1e18 while low <= high: mid = (low + high) // 2 print(f"? {mid}") response = input() if response == ">=": low = mid + 1 else: high = mid - 1 print(f"! {high}") ``` - **注意事项**: - 刷新输出缓冲区(C++用`cout << endl`) - 错误处理:CF 1520F2(二进制猜数容错机制) 3. **数据结构题** - **题型矩阵**: | 数据结构 | 适用场景 | 优化技巧 | |----------------|-------------------------------|---------------------------| | 线段树 | 区间查询/更新 | 延迟标记(lazy propagation)| | 并查集 | 连通性检测 | 路径压缩+按秩合并 | | 字典树 | 前缀匹配统计 | 二进制位处理 | | 单调队列 | 滑动窗口最值 | 淘汰非最优候选 | #### 三、实战技巧体系 1. **复杂度预判法**: - 输入规模$n$与可接受时间复杂度对应表: $$ \begin{array}{c|c} n \leq 10^6 & O(n) \\ n \leq 10^5 & O(n \log n) \\ n \leq 10^4 & O(n^2) \\ n \leq 20 & O(2^n) \\ \end{array} $$ 2. **Debug技巧**: - 边界测试:$n=0,1$等极端情况 - 对拍验证:生成随机数据与暴力程序对比 - 输出中间状态:使用`cerr`输出调试信息 3. **代码模板管理**: - 建议维护以下模板库: ```text /templates ├── graph │ ├── dijkstra.cpp │ └── tarjan_scc.cpp ├── math │ ├── matrix_fastpow.cpp │ └── fft_template.cpp └── ds ├── segment_tree.cpp └── union_find.cpp ``` #### 四、经典例题解析 1. **CF 1485D - Multiples and Power Differences** - **题型**:构造题+数论 - **关键思路**: 构造矩阵元素$a_{i,j} = 720720 + (i+j)\%2 \times (a_{i,j}^4)$ 保证相邻元素差为四次方数 2. **CF 1513C - Add One** - **解法**:动态规划预处理 定义$dp[d][m]$表示数字$d$经过$m$次操作的位数 状态转移: $$ dp[d][m] = \begin{cases} 1 & m=0 \\ dp[1][m-1] + dp[0][m-1] & d=9 \\ dp[d+1][m-1] & otherwise \end{cases} $$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值