题目1 : Composition

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

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

由于删掉最少等价于留下最多,所以我们可以用f[i]表示最后留下一个字符是S[i]时,最多留下多少个字符。

要求f[i],我们只需要枚举S[i]之前的一个字符是哪个,并且从中选择合法并且最优的解:

f[i] = max{f[j]} + 1, for j = 1 .. i-1且S[j]S[i]不是"illegal pair"。

以上的DP解法是O(N^2)的,仍有优化的空间。

我们求f[i]时,需要枚举之前所有的位置j,但实际上之前的字符只有'a'~'z'26种可能。对于相同的字符,我们只用考虑最后一次它出现的位置,之前的位置一定不是最优。

例如,假设s[2] = s[5] = s[8] = 'h',那么当i>8时,我们求f[i]根本不用考虑j=2和j=5的情况,这两种情况即便合法也不会比j=8更优。

于是我们每次求f[i]只需要考虑'a'~'z'最后一次出现的位置。复杂度可以优化到O(26N)

import java.io.BufferedInputStream;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;public class Main {public static final int INF = 0x3f3f3f3f;public static Scanner in = new Scanner(new BufferedInputStream(System.in));public static void main(String[] args) {String string;boolean[][] map = new boolean[27][27];for(int i='a' ; i<='z' ; ++i)for(int j='a' ; j<='z' ; ++j)map[i-'a'][j-'a'] = true;in.nextLine();string = in.nextLine();while (string=="")string = in.nextLine();int k = in.nextInt();while (k-->0) {String string2 = in.nextLine();while (string2.equals(""))string2 = in.nextLine();int u = string2.charAt(0)-'a';int v = string2.charAt(1) - 'a';map[u][v] = map[v][u] = false;}System.out.println(new Solution(string, map).solve());}
}
class Solution {String string;boolean[][] map;int[] cnt;public Solution(String string, boolean[][] map) {super();this.string = string;this.map = map;cnt = new int['z'+10];for(int i='a' ; i<='z' ; ++i)cnt[i] =0;}public int solve() {for(int i=0 ; i<string.length() ; ++i){char now = string.charAt(i);int tmp =0;for(int j='a' ; j<='z' ; ++j){if(map[now-'a'][j-'a'])tmp = Math.max(tmp, cnt[j]+1);}cnt[now] = Math.max(tmp, cnt[now]);}int ret =0;for(int i='a' ; i<'z' ; ++i)ret = Math.max(ret, cnt[i]);return string.length()-ret;}
}

Composition相关推荐

  1. UWP Composition API - 锁定列的FlexGrid

    原文:UWP Composition API - 锁定列的FlexGrid 需求是第一列锁定,那么怎么让锁定列不跟着滚动条向做移动呢? 其实很简单,让锁定列跟scrollviewer的滚动做反方向移动 ...

  2. Aggregation,Composition和Dependency

    两个类之间的关系,例如类A和B. 如果是B是A成员变量,而且B在A的构造函数中生成(new),那么就是Composition. 如果是B是A成员变量,而且B不在A的构造函数中生成(new),而是在有需 ...

  3. UWP Composition API - PullToRefresh

    原文:UWP Composition API - PullToRefresh 背景: 之前用ScrollViewer 来做过 PullToRefresh的控件,在项目一些特殊的条件下总有一些问题,比如 ...

  4. 论文阅读 Current Solutions for Web Service Composition

    简单信息 Title Current solutions for Web service composition Journal IEEE Internet Computing Year 2004 A ...

  5. 【Win 10 应用开发】UI Composition 札记(二):基本构件

    在上一篇中,老周用一个示例,演示了框架视图的创建过程,在本篇中,老周将给大伙伴们说一下 Composition 构建 UI 的一些"零件". UI Composition 有一个核 ...

  6. UWP Composition API - RadialMenu

    原文:UWP Composition API - RadialMenu 用Windows 8.1的童鞋应该知道OneNote里面有一个RadialMenu.如下图,下图是WIn10应用Drawboar ...

  7. 组合模式(Composition)

    组合模式(Composition)   组合模式(Composition) 意图:将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性. ...

  8. UWP Composition API - GroupListView(一)

    需求: 光看标题大家肯定不知道是什么东西,先上效果图: 这不就是ListView的Group效果吗?? 看上去是的.但是请听完需求. 1.Group中的集合需要支持增量加载ISupportIncrem ...

  9. WPF 问题 PresentationCore.dll!System.Windows.Media.Composition.DUCE.Channel.SyncFlush() 分析

    错误信息: 没有足够的内存继续执行程序在 System.Windows.Media.Composition.DUCE.Channel.SyncFlush()在 System.Windows.Inter ...

  10. SAP CRM OData multiple origin Composition的测试

    Sent: Wednesday, December 3, 2014 2:48 PM Subject: Multiple Origin composition test - Opportunity Cr ...

最新文章

  1. 探秘重编译(Recompilations)(1/2)
  2. 深度学习 占用gpu内存 使用率为0_深度学习的完整硬件指南
  3. VTK:图像收缩用法实战
  4. HDU 5115 Dire Wolf
  5. 三维重建12:室内三维物体的位姿识别论文列表
  6. sql调优的几种方式_「数据库调优」屡试不爽的面试连环combo
  7. yolov5鱼苗检测计数:从数据标注到训练
  8. Python 玩转数据 17 - Pandas 数据处理 合并 pd.merge() df1.merge(df2)
  9. php奖学金系统,java/php/net/pythont奖助学金管理系统设计
  10. leetcode79 word serach 解题报告
  11. matlab r2021b校园正版软件安装过程的问题及解决方案记录
  12. css文字多余显示,css设置文字多余部分显示省略号
  13. [论文阅读] Meta-Weight-Net: Learning an Explicit Mapping For Sample Weighting
  14. zotero+坚果云
  15. 百度地图实现鼠标绘制多边形并获取所有点坐标
  16. 【开关电源】BUCK和BOOST变换器电感的设计
  17. Resnet_50网络结构详解
  18. BGP的路由优选规则
  19. Theano的安装及GPU的配置
  20. 在vue中使用Google Recaptcha验证

热门文章

  1. 使用idea在serviceImpl中配置radis
  2. PR剪辑视频画面翻转缩放滑动特效转场PR预设
  3. “狗屁不通”文章生成器网页版代码
  4. 一个Web前端实习生的简历
  5. web 系统发展历程
  6. html、css 实现网页弹出层
  7. 计算机关机后耗电问题,笔记本关机后耗电严重?Win10笔记本关机还耗电解决办法...
  8. Word排版之页眉页脚设置
  9. 2021职业技能鉴定2021维修电工证(中级)考试题
  10. 百度、谷歌搜索引擎原理