题目

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的更多相关文章

  1. 微软2017校招笔试题3 registration day

    题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...

  2. 微软2014校招笔试题-String reorder

    Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB Description For this question, your pro ...

  3. 剑指Offer——腾讯+360+搜狗校招笔试题+知识点总结

    剑指Offer--腾讯+360+搜狗校招笔试题+知识点总结 9.11晚7:00,腾讯笔试.选择题与编程.设计题单独计时. 栈是不是顺序存储的线性结构啊? 首先弄明白两个概念:存储结构和逻辑结构. 数据 ...

  4. 剑指Offer——京东校招笔试题+知识点总结

    剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...

  5. 剑指Offer——美团内推+校招笔试题+知识点总结

    剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...

  6. 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)

    剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...

  7. C# - 2017微软校园招聘笔试题 之 MS Recognition[待解决]

    MS Recognition 在线提交: hihoCoder 1402 http://hihocoder.com/problemset/problem/1402 类似: OpenJudge - I:P ...

  8. hiho #1288 微软2016.4校招笔试题 Font Size

    #1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...

  9. 2016京东Android研发校招笔试题

    一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...

随机推荐

  1. 系统隐式 Intent

    1. 找出系统中所有视频 private void choiceFile() { Intent intent = new Intent(Intent.ACTION_PICK, android.prov ...

  2. curl 查看一个web站点的响应时间(rt)

    1. curl 查看web站点rt curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer} ...

  3. 移动web开发框架

    纯粹的总结一下移动web开发框架,移动web开发框架有jQuery Mobile .Sencha Touch等等,他们都来源于web开发,是成熟的框架,jQuery Mobile出自于jQuery家族 ...

  4. XSS (Cross Site Scripting) Prevention Cheat Sheet(XSS防护检查单)

    本文是 XSS防御检查单的翻译版本 https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sh ...

  5. a c lang in linux

    create shortcut: ln -s sourcepath -destdirectoryln -s /home/tell/calos /home/桌面 #include <iostrea ...

  6. 提取SD卡中的图片

    读取SD卡的权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> ...

  7. C++学习笔记(1)——数据类型占空间大小

    boolean bool 1 byte   character char 1 byte May be signed or unsigned   wchar_t 1 byte     char16_t ...

  8. WIN7下java环境的搭建

    首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入“计算机 ...

  9. win api 实现 AES加密、解密

    WCHAR szPasswod[] = {L"goodpasswod" }; HCRYPTHASH hHash; HCRYPTPROV hCryptProv; HCRYPTKEY ...

  10. maven、strutst版本号

    struts.2.3.15.3 :大版本号 struts1-->struts2 :分支版本号 增加新功能 :小版本号 修复bug beta:公测版release:发行版(已可以使用)Genera ...