acm专题---动态规划
题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096
#1400 : Composition
描述
Alice writes an English composition with a length of N characters. However, her teacher requires that M illegal pairs of characters cannot be adjacent, and if 'ab' cannot be adjacent, 'ba' cannot be adjacent either.
In order to meet the requirements, Alice needs to delete some characters.
Please work out the minimum number of characters that need to be deleted.
输入
The first line contains the length of the composition N.
The second line contains N characters, which make up the composition. Each character belongs to 'a'..'z'.
The third line contains the number of illegal pairs M.
Each of the next M lines contains two characters ch1 and ch2,which cannot be adjacent.
For 20% of the data: 1 ≤ N ≤ 10
For 50% of the data: 1 ≤ N ≤ 1000
For 100% of the data: 1 ≤ N ≤ 100000, M ≤ 200.
输出
One line with an integer indicating the minimum number of characters that need to be deleted.
样例提示
Delete 'a' and 'd'.
- 样例输入
-
5
abcde
3
ac
ab
de - 样例输出
-
2
#include <iostream>
using namespace std;
#include <vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<math.h>
#include<iomanip>
#include<stack>
int main()
{
int n;
cin>>n;
string str;
cin>>str;
int m;
cin>>m;
bool flag[26][26];
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
flag[i][j]=true;
}
for(int i=0;i<m;i++)
{
string tmp;
cin>>tmp;
flag[tmp[0]-'a'][tmp[1]-'a']=flag[tmp[1]-'a'][tmp[0]-'a']=false;
}
int dp[26];
for(int i=0;i<26;i++)
dp[i]=0;
dp[0]=1;
for(int i=1;i<n;i++)
{
int maxnum=0;
for(int j=0;j<26;j++)
{
if(flag[str[i]-'a'][j])
maxnum=max(maxnum,dp[j]+1);
}
dp[str[i]-'a']=maxnum;
}
int ans=0;
for(int i=0;i<26;i++)
{
if(dp[i]>ans)
ans=dp[i];
}
cout<<n-ans<<endl;
return 0;
}
acm专题---动态规划的更多相关文章
- acm专题---拓扑排序+优先队列
struct node{ int id; int cnt; node(int _id,int _cnt):id(_id),cnt(_cnt){} bool operator<(node a) c ...
- acm专题---最小生成树
kruscal(eloge): 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N ...
- acm专题---最短路
spfa的时间复杂度是0(e) 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅 ...
- acm专题---KMP模板
KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m) 觉得大话数据结果上面这个讲得特别好 改进版本的KMP leetcode 28. Implement strS ...
- acm专题--并查集
题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...
- acm专题---dfs+bfs
题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...
- acm专题---键树
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
- ACM:动态规划,01背包问题
题目: 有n件物品和一个容量为C的背包.(每种物品均仅仅有一件)第i件物品的体积是v[i],重量是w[i].选一些物品装到这个背包中,使得背包内物品在整体积不超过C的前提下重量尽量大. 解法:两种思路 ...
- 收集一些关于OI/ACM的奇怪的东西……
一.代码: 1.求逆元(原理貌似就是拓展欧几里得,要求MOD是素数): int inv(int a) { if(a == 1) return 1; return ((MOD - MOD / a) * ...
随机推荐
- 洛谷 U14475 部落冲突 【比赛】 【树链剖分 + 线段树】
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
- skip-external-locking --mysql配置说明
MySQL的配置文件my.cnf中默认存在一行skip-external-locking的参数,即“跳过外部锁定”.根据MySQL开发网站的官方解释,External-locking用于多进程条件下为 ...
- 【CF123E】Maze
Portal --> cf123E Solution 首先步数的话可以转化成每条边经过了几次这样来算 假设现在确定了起点\(S\)和终点\(T\),我们将\(T\)看成树根,那么考虑边\((u, ...
- 【OpenCV】SIFT原理与源码分析:方向赋值
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一篇<关键点搜索与定位>,我们已经找到 ...
- 对于redis框架的理解(四)
上一篇讲述了eventloop的结构和创建,添加文件事件删除文件事件,派发等等. 而eventloop主要就是调用不同网络模型完成事件监听和派发的. 这一篇主要讲述epoll网络模型,redis是如何 ...
- linux 内存计算
原文: http://www.open-open.com/lib/view/open1424325362577.html Linux中的Cache Memory 什么是Cache Memory(缓存内 ...
- docker操作mysql
Docker操作mysql 查找docker hub上的mysql镜像 Docker search.mysql 拉取官方的镜像标签为5.6 Docker pull mysql:5.6 在本地镜像列表里 ...
- oracle分析函数 (转)
一.总体介绍 12.1 分析函数如何工作 语法 FUNCTION_NAME(<参数>,…) OVER (<PARTITION BY 表达式,…> <ORDER BY 表达 ...
- 2017-2018-2 20179207 《网络攻防技术》第十三周作业 python3实现SM234算法
国密算法SM234 的python3实现 国家标准 GM/T 0002-2012 <SM4分组密码算法> GM/T 0003.1-2012 <SM2椭圆曲线公钥密码算法 第1部分:总 ...
- Codeforces 221 C. Little Elephant and Problem
C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...