Codeforces 814C An impassioned circulation of affection(dp)
来源:互联网 发布:mac梦幻西游启动失败 编辑:程序博客网 时间:2024/06/06 02:17
题目Codeforces 814C
题意:给定长为n的字符串,有q次询问,每次询问能在原字符串中更改m个字母,使更改后的字符串中指定字母连续长度最长,输出最长长度。
思路:dp[k][i][j]表示指定字母为k,以第i位为连续的结尾,修改过j次的最长长度。状态转移方程见代码。首先对所有查询字母和次数初始化,记录答案,然后输出即可。
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <cmath>#include <vector>#include <iostream>#include <stack>#include <set>#include <map>using namespace std;const int MAX=1505;int n,m,q,dp[27][MAX][MAX],ans[30][MAX];char c,str[MAX];void solve(void){int i,j,k;for(k=0;k<26;k++){for(i=1;i<=n;i++){for(j=0;j<=i;j++){if(str[i]-'a'==k){dp[k][i][j]=1;dp[k][i][j]=max(dp[k][i][j],dp[k][i-1][j]+1);if(j-1>=0) //不可忽略 当循环i-1时j无法取到i 可看作字母符合时也要修改一次 dp[k][i][j]=max(dp[k][i][j],dp[k][i-1][j-1]+1);}else{dp[k][i][j]=0;if(j-1>=0) dp[k][i][j]=max(dp[k][i][j],dp[k][i-1][j-1]+1);}}}}for(k=0;k<26;k++){for(j=0;j<=n;j++){for(i=1;i<=n;i++){ans[k][j]=max(ans[k][j],dp[k][i][j]);}}}}int main(){int i;scanf("%d%s%d",&n,str+1,&q);solve();while(q--){scanf("%d %c",&m,&c);printf("%d\n",ans[c-'a'][m]);}return 0;}
阅读全文
0 0
- Codeforces 814C An impassioned circulation of affection【Dp】
- Codeforces 814C An impassioned circulation of affection (dp)
- Codeforces 814C An impassioned circulation of affection(dp)
- codeforces C. An impassioned circulation of affection
- 【Codeforces 814 C. An impassioned circulation of affection】+ 尺取法
- codeforces 814C An impassioned circulation of affection
- codeforces 814C An impassioned circulation of affection
- codeforces 814 C An impassioned circulation of affection
- Codeforces 814C-An impassioned circulation of affection
- codeforces 814 C. An impassioned circulation of affection(暴力)
- codeforces 814C An impassioned circulation of affection
- Codeforces 814 C An impassioned circulation of affection
- CodeForces-814C An impassioned circulation of affection
- 814C An impassioned circulation of affection
- Codeforces#418C An impassioned circulation of affection
- Codeforces Round #418 C--An impassioned circulation of affection
- C. An impassioned circulation of affection-尺取法或者DP
- Codeforces#418 An impassioned circulation of affection
- 区块链开发(五) Browser-solidity
- 安装jdk,jre及配置java运行环境
- Linux下ftp的用户配置
- Maven中央仓库——你可能不知道的细节
- 【python数据挖掘课程】十九.鸢尾花数据集可视化、线性回归、决策树花样分析
- Codeforces 814C An impassioned circulation of affection(dp)
- [BZOJ4911][SDOI2017]切树游戏-链分治-快速沃尔什变换
- 自顶而下?只是发生了一个分解反应
- form表单的两种提交方式,submit和button的用法
- 无聊的写一写C++ primier感想
- C++ 数字转字符串
- codeforces834D
- 杨辉三角
- produces在@requestMapping中的使用方式和作用