微软2017校招笔试题2 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.
给定一个由小写字母组成的字符串,规定某些字符不能相邻。求最少需要删除多少个字符使得剩下的字符串中不存在那些规定不能相邻的字符相邻。
解法
字符串中的字符只有26种可能,去除某些字符剩余的字符串只能以a-z这些字符结尾,如果记录 dp[c]表示以 字符 c+'a' 结尾的字符串的最长长度,则可以有递推公式
dp[ch] = max(dp[ch], dp[t] + 1) t为a-z,且t和ch可以相邻。这样,在从头到尾扫描完一遍字符串,得到dp数组之后,最少需要删除的字符的个数就等于 n(原来字符串长度) - max(dp[c]).
#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<functional>
#include<queue>
#include<vector>
#include<set>
#include<list>
#include<unordered_map>
#include<unordered_set>
#include<stack>
#include<map>
#include<algorithm>
#include<string.h>
using namespace std;
char str[1000005];
int dp[26];
int n;
bool table[26][26];
int main(){
scanf("%d", &n);
scanf("%s", str);
memset(dp, -1, sizeof(dp));
int k;
scanf("%d", &k);
char word[4];
for (int i = 0; i < k; i++){
scanf("%s", word);
table[word[0] - 'a'][word[1] - 'a'] = true;
table[word[1] - 'a'][word[0] - 'a'] = true;
}
for (int i = 0; i < n; i++){
int cur = str[i] - 'a';
int tmp = 1;
for (int j = 0; j < 26; j++){
if (table[cur][j])
continue;
tmp = max(tmp, dp[j] + 1);
}
dp[cur] = tmp;
}
int tmp = 0;
for (int i = 0; i < 26; i++)
tmp = max(tmp, dp[i]);
printf("%d\n", n - tmp);
return 0;
}
微软2017校招笔试题2 composition的更多相关文章
- 微软2017校招笔试题3 registration day
题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...
- 微软2014校招笔试题-String reorder
Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB Description For this question, your pro ...
- 剑指Offer——腾讯+360+搜狗校招笔试题+知识点总结
剑指Offer--腾讯+360+搜狗校招笔试题+知识点总结 9.11晚7:00,腾讯笔试.选择题与编程.设计题单独计时. 栈是不是顺序存储的线性结构啊? 首先弄明白两个概念:存储结构和逻辑结构. 数据 ...
- 剑指Offer——京东校招笔试题+知识点总结
剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...
- 剑指Offer——美团内推+校招笔试题+知识点总结
剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...
- 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)
剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...
- C# - 2017微软校园招聘笔试题 之 MS Recognition[待解决]
MS Recognition 在线提交: hihoCoder 1402 http://hihocoder.com/problemset/problem/1402 类似: OpenJudge - I:P ...
- hiho #1288 微软2016.4校招笔试题 Font Size
#1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...
- 2016京东Android研发校招笔试题
一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...
随机推荐
- 《Linux内核设计与实现》CHAPTER5阅读梳理
<Linux内核设计与实现>CHAPTER5阅读梳理 [学习时间:2.5hours] [学习内容:系统调用的概念.功能及实现:系统调用的创建和使用方法] CHAPTER5 系统调用 1.系 ...
- angularJs内置指令63个
- free-library-converts-2d-image-to-3d
http://www.i-programmer.info/news/105-artificial-intelligence/4917-free-library-converts-2d-image-to ...
- 解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”问题
解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”问题 今天由于测试需要,在网上下载了Adventureworks2008实例数据库的BAK文件,进行还原时出现了这样的错误“F ...
- c# signalr聊天室开源资料
SignalR+LayIM源码: http://www.cnblogs.com/panzi/p/5742089.html 钉钉客户端源码: http://www.cnblogs.com/loveson ...
- Asp.Net alert 方法
public static void ExcuteAlert(Page page, string strAlerts) { ClientScriptManager ...
- 搭建springmvc的步骤
一定注意文件的层级关系 1.cope相关的jar包到web-inf/lib目录下,并添加到类路径中. 2.配置web.xml文件,配置前端控制器DisPatcherServlet <servle ...
- System.arraycopy
ref : http://blog.csdn.net/jaycee110905/article/details/45228249
- Java多线程编程——进阶篇一
一.线程栈模型与线程的变量 要理解线程调度的原理,以及线程执行过程,必须理解线程栈模型. 线程栈是指某一时刻内存中线程调度的栈信息,当前调用的方法总是位于栈顶.线程栈的内容是随着程序的运行动态变化的, ...
- ARM9代码分析启动MAIN.C
#define GLOBAL_CLK 1 #include <stdlib.h> #include <string.h> #include “def.h” #include “ ...