【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)

题干:

Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.

Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.

Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".

Input

The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string sdoesn't exceed 105.

The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters.

Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.

Output

Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.

Examples

Input

ababa
1
ab

Output

2

Input

codeforces
2
do
cs

Output

1

Note

In the first sample you should remove two letters b.

In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.

题目大意:

   在一串文字中有k个字符对(2位)不允许出现,问最小修改数。输入时的字符对保证两个字符不同,且其中字符唯一

解题报告:

    一个字串只含有某个对中的字母,这个字符对才可能存在,要破坏这个字串,就在改变出现次数最少的那个字符,由此寻找符合题意的字串,并且求出修改次数,叠加即可。(主要是这题说明了,这k对字符串都不是重复字符)

既然k个两字符串中没有重复的串,那么你删除一些必要的字符后肯定不会造成新的不符合要求的串,所以只要统计下串中连续的不合法的字符的个数,取最小值就可以了。

    关于造数据:比如第2个样例的k对,也需要考虑一下类似于addcosd这种岔开的,(其实也没事,因为禁止串的两个字符不会同时消除,所以不会产生新的禁止串)

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;char s[100000 + 5]; 
int main()
{int k,cnt1,cnt2;char tmp[15];ll ans = 0;cin>>s;int len = strlen(s);cin>>k;while(k--) {cin>>tmp;if(tmp[0] == tmp[1]) {//其实这题不用考虑这个for(int i = 0; i<len; i++) {while(s[i] == tmp[0] && i<len) ans++,i++;ans--;}continue;} cnt1=cnt2=0;for(int i = 0; i<len; i++) {cnt1=cnt2=0;while(s[i] == tmp[0] || s[i] == tmp[1]) {if(s[i] == tmp[0]) cnt1++;if(s[i] == tmp[1]) cnt2++;i++;}ans += min(cnt1,cnt2);}}printf("%lld\n",ans);return 0;
}

或者这样写:(熟悉这种if -> while句式)(很多地方都会用到,比如求约数和【 HDU - 1215 】七夕节(数论,约数和公式))

if(tmp[0] == tmp[1]) {for(int i = 0; i<len; i++) {if(s[i] == tmp[0] && i<len) {i++;while(s[i] == tmp[0] && i<len) ans++,i++;}}continue;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441558.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html 报表插件,轻量级图形报表插件JSCharts

插件描述&#xff1a;JScharts是一个用于在浏览器直接绘制图表的JavaScript工具包。JScharts支持柱状图、圆饼图以及线性图&#xff0c;可以直接将这个图插入网页&#xff0c;JScharts图的数据可以来源于XML文件、JSON文件或JavaScript数组变量。2017-05-09更新&#xff1a;改为…

算法讲解 -- 二分图之 匈牙利算法

匈牙利算法是由匈牙利数学家Edmonds于1965年提出&#xff0c;因而得名。匈牙利算法是基于Hall定理中充分性证明的思想&#xff0c;它是部图匹配最常见的算法&#xff0c;该算法的核心就是寻找增广路径&#xff0c;它是一种用增广路径求二分图最大匹配的算法。 -------等等&…

html 表格文字颜色 css,CSS 表格-JavaScript中文网-JavaScript教程资源分享门户

使用 CSS 可以使 HTML 表格更美观。CompanyContactCountryAlfreds FutterkisteMaria AndersGermanyBerglunds snabbkpChristina BerglundSwedenCentro comercial MoctezumaFrancisco ChangMexicoErnst HandelRoland MendelAustriaIsland TradingHelen BennettUKKniglich EssenP…

计算机专业表白文案,深情表白文案40句

深情表白文案40句时间&#xff1a;2020-05-17 22:41:51 分类&#xff1a;经典语句 | 适合表白的句子 | Word文档下载深情表白文案40句导语&#xff1a;你是我生活的全部&#xff0c;是我一生拼搏奋斗的赌注。为了你披荆斩棘&#xff0c;为了你艰辛受苦&#xff0c;为了你众生…

【HRBUST - 1054 】Brackets! Brackets! (括号匹配,思维,STL栈)

题干&#xff1a; There are six kinds of brackets: ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{’, ‘}’. dccmx’s girl friend is now learning java programming language, and got mad with brackets! Now give you a string of brackets. Is it valid? For example: “(([{}]))…

计算机中有关数及编码的知识,计算机中有关数及编码的知识

计算机中有关数及编码的知识1.计算机是智能化的电器设备计算机就其本身来说是一个电器设备&#xff0c;为了能够快速存储、处理、传递信息&#xff0c;其内部采用了大量的电子元件&#xff0c;在这些电子元件中&#xff0c;电路的通和断、电压高低&#xff0c;这两种状态最容易…

ACM技巧 - O(1)快速乘(玄学) 总结

ll fast_mult(ll a,ll b,ll mod) {return (a*b - (ll)((long double)a/mod*b)*modmod)%mod; } 参考&#xff1a;https://blog.csdn.net/m0_38013346/article/details/81435369 注意事项&#xff1a; double可能会挂&#xff0c;最好long double。u,v>p可能会挂&#xff0…

html盒子阴影只设置左右,只在容器一边或两边显示盒子阴影

实现方案首先定义一个有具体宽高的盒子&#xff0c;然后正确定位:after伪类。效果[图片上传中...(image.png-8e8e17-1593570486291-0)]底部阴影代码:Document.box-shadow {background-color: #FF8020;width: 160px;height: 90px;position: relative;}.box-shadow:after {conten…

【 HRBUST - 1055】Single(模拟,dp,打表)(总结)

题干&#xff1a; There are many handsome single boys in our team, for example, me. Some times, we like count singles. For example, in the famous “November 11th” (11.11), there are four singles ,so, single is actually 1. For another example, there are 2 …

一年级下册数学计算机应用题,【小学一年级数学练习题】小学生和机器人

【www.jmzhongda.cn--学生个人工作总结】一个小学生在认真地做作业。这是一系列加、减、乘、除的四则应用计算题&#xff0c;难度相当大&#xff0c;特别那几个繁分数题&#xff0c;计算起来太繁杂。他额头上不知不觉地渗出细雨般的汗珠来了。正在这个时候&#xff0c;不知从什…

【EOJ Monthly 2018.10 - A】oxx 的小姐姐们(模拟,水题,填充矩阵,输出格式有坑)

题干&#xff1a; Time limit per test: 1.0 seconds Memory limit: 512 megabytes oxx 和他的小姐姐&#xff08;们&#xff09;躺在图书馆前的大草坪上看星星。 有强迫症的 oxx 想要使得他的小姐姐们正好躺成一块 nm 的长方形。 已知小姐姐的形状是 1p 的长方形&#xf…

数据结构数组计算机中的应用,2018考研计算机:数据结构数组和广义表复习重点...

2018考研计算机&#xff1a;数据结构数组和广义表复习重点2017-08-17 16:00|考研集训营《数据结构(C语言版)》复习重点在二、三、六、七、九、十章&#xff0c;考试内容两大类&#xff1a;概念&#xff0c;算法&#xff0c;自从计算机专业课统考以后&#xff0c;专业课考试题型…

【HDU - 5672】String(尺取法)

题干&#xff1a; There is a string SS.SS only contain lower case English character.(10≤length(S)≤1,000,000)(10≤length(S)≤1,000,000) How many substrings there are that contain at least k(1≤k≤26)k(1≤k≤26) distinct characters? Input There are mul…

【HDU - 2030 】汉字统计 (C语言汉字编码,长知识)

题干&#xff1a; 统计给定文本文件中汉字的个数。 Input 输入文件首先包含一个整数n&#xff0c;表示测试实例的个数&#xff0c;然后是n段文本。 Output 对于每一段文本&#xff0c;输出其中的汉字的个数&#xff0c;每个测试实例的输出占一行。 [Hint:]从汉字机内码的…

【HDU - 1257】最少拦截系统 (标解dp,贪心可过,最长上升子序列类问题)

题干&#xff1a; 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能…

ae toolbarcontrol运行时没有_想办法让AE跑起来

好像大家都已经习惯了ADOBE系列软件的高能量吸收&#xff0c;谁让人家功能强大呢&#xff0c;尤其是AE&#xff0c;发展势头越来越猛&#xff0c;目前可以直接生成JSON文件&#xff0c;解析为代码文件后跨平台使用于安卓和苹果设备上&#xff0c;算是直接搭上了火热的移动互联快…

C++中两个常用的控制语句格式的函数(width和precision函数)

width 语法: int width();int width( int w );函数 width()返回当前的宽度。可选择参数w用于设定宽度大小。宽度是指每一次输出中显示的字符的最小数目。例如&#xff1a; cout.width( 5 );cout << "2";displays 2(在一个2的后面紧跟着四个空格) precision 语…

如何将风险应用加入白名单_理财要如何将资金分散呢?可以参考风险分散和分享经济红利的思路...

#理财大赛第三季#银行理财是值得投资者配置资金的&#xff0c;收益相对稳定&#xff0c;它的风险等级也很明显地标注在产品说明书和风险揭示书上&#xff0c;投资者可以根据自身投资偏好选择适合的理财。大部分买过银行理财的人都会选择将所有资金投入进去。银行理财确实是一个…

【洛谷 - 快来签到】(数学,公式,溢出爆longlong的处理)

题干&#xff1a; 题目描述 sum2581114…&#xff0c;输入正整数n&#xff0c;求sum的前n项和。 输入输出格式 输入格式&#xff1a; 输入一个正整数n 输出格式&#xff1a; 输出一个整数表示答案 输入输出样例 输入样例#1&#xff1a; 复制 2 输出样例#1&#xff1a; 复…

bind merge r 和join_[R] 制作梅西和C罗进球数的quot;追赶动画quot; - ggplot2 + gifski

效果如下&#xff1a;数据可视化 - 梅西 vs C罗https://www.zhihu.com/video/1084910827596804096数据可视化 - 8大射手进球趋势https://www.zhihu.com/video/1084910854461321216制作过程分为3个步骤:处理数据ggplot2创建图像帧save_gif逐帧打包生成gif文件使用的packages:lib…