xtu summer individual 2 C - Hometask
Hometask
This problem will be judged on CodeForces. Original ID: 154A
64-bit integer IO format: %I64d Java class name: (Any)
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 s doesn'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.
Sample Input
ababa
1
ab
2
codeforces
2
do
cs
1
Hint
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.
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
char str[],s[];
int main(){
int n,i,j,ans,lt,rt;
while(~scanf("%s",str)){
scanf("%d",&n);
for(ans = i = ; i < n; i++){
scanf("%s",s);
for(j = ; str[j]; j++){
lt = rt = ;
while(str[j] == s[] || str[j] == s[]){
if(str[j] == s[]) lt++;
else rt++;
j++;
}
ans += min(lt,rt);
}
}
printf("%d\n",ans);
}
return ;
}
xtu summer individual 2 C - Hometask的更多相关文章
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 1 A - An interesting mobile game
An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...
- xtu summer individual 2 D - Colliders
Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- xtu summer individual 1 D - Round Numbers
D - Round Numbers Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u D ...
- xtu summer individual 5 F - Post Office
Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...
随机推荐
- 线段树+离散化 POJ 2528 Mayor's posters
题目传送门 题意:在一面墙上贴海报,有先后顺序,问最后有多少张不同的海报(指的是没被覆盖或者只是部分覆盖的海报) 分析:这题数据范围很大,直接搞超时+超内存,需要离散化:离散化简单的来说就是只取我们需 ...
- LVS实现负载均衡
三台主机模拟 sishen_63(分发器): eth0(Bridge):192.168.1.63 eth1(vmnet4):192.168.2.63 sishen_64(RealServer1): e ...
- Spring Boot学到的内容
Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
- web前端工程师入门须知
本文是写给那些想要入门web前端工程的初学者,高手请路过,也欢迎高手们拍砖. 先说下web前端工程师的价值,目前web产品交互越来越复杂,用户使用体验和网站前端性能优化这些都得靠web前端工程师去做w ...
- 【HEVC简介】DB-DeBlock Filter
参考论文:HEVC Deblocking Filter <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com/DwyaneTalk/ ...
- Web开发者不容错过的10个HTML5工具
HTML5已经成为当今世界的一个必然组成部分.由于World Wide Web万维网是使用超文本标记语言来架构和呈现的,于是HTML5成为了最流行的编程语言之一.随着网络的不断扩张,Web开发人员非常 ...
- Android(java)学习笔记183:多媒体之图形颜色的变化
1.相信大家都用过美图秀秀中如下的功能,调整颜色: 2. 下面通过案例说明Android中如何调色: 颜色矩阵 ColorMatrix cm = new ColorMatrix(); paint.se ...
- JAVA用freemarker生成复杂Excel。(freemarker)
在生成Excel的时候,大多时候都是使用poi,jxl等进行的,但是对于复杂的Excel来说,这个工作量是非常的大的,而且,对于我这么懒的人来说,这是相当痛苦的一件事情,所以,我不得不找找有没有简单一 ...
- L2-2 社交集群 (25 分)(一个写挫的并查集)
题目: 思路: 就是一个并查集的裸题,不过在数据查找方面可能不好处理,暴力完全可以解决这个问题啊!! #include <bits/stdc++.h> #include <cstdi ...