CF154A Hometask

C. 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 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.

Examples

Input

ababa1ab

Output

2

Input

codeforces2docs

Output

1

贪心

题目自身限制每个字母最多出现在一个限制词语中,给出k个限制词语,将问题转化为k个子问题,对每个限制词语遍历给出的字符串,如限制词do,若出现连续的oddoood(连续的o和d),统计o和d出现的次数,消去较小的次数。

#include<cstdio>
#include<cstring>
using namespace std;
const int N=100005;
char fb[14][2];
char s[N];
int main() {
    int n;
    gets(s);
    scanf("%d\n",&n);
    int len=strlen(s);
    for(int i=1;i<=n;i++) {
        scanf("%c%c",&fb[i][0],&fb[i][1]);
        getchar();
    }
    int cnt=0,cntt=0,res=0;
    for(int j=1;j<=n;j++) {
        cnt=0,cntt=0;
        for(int i=0;i<len;i++) {
            if(s[i]==fb[j][0] || s[i]==fb[j][1]) {
                if(s[i]==fb[j][0]) cnt++;
                else if(s[i]==fb[j][1]) cntt++;
            }
            else {
                res+=min(cnt,cntt);
                cnt=cntt=0;
            }
        }
        if(s[len-1]==fb[j][0] || s[len-1]==fb[j][1]) res+=min(cnt,cntt);
    }
    printf("%d\n",res);
    return 0;
} 
时间: 2024-09-28 21:47:13

CF154A Hometask的相关文章

xtu summer individual 2 C - Hometask

Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 154A64-bit integer IO format: %I64d      Java class name: (Any) Sergey attends lessons of the N-ish language. Each lesson he receives a hometas

Codeforce 214 Div 2 B.Hometask

题目描述: Description Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? Y

Moscow Subregional 2010 Problem H. Hometask 转化、素数筛选

ACM ICPC 2010-2011 NEERC Moscow Subregional Contest Moscow, October 24, 2010 Problem H. Hometask Time limit: 1 second Memory limit: 256 megabytes Kolya is still trying to pass a test on Numbers Theory. The lecturer is so desperate about Kolya's knowl

CodeForces 214B Hometask

本题求n个数组成的大数,要求是2,3,5的倍数. 因为是2 和5 的倍数,所以个位为 0:所以若n个数中没有0,直接输出-1: 难点就是要求为3 的倍数. 因为若某个数为3的倍数,则其各位数的和必然是3的倍数. 当n个数的和为3的倍数时从大到小输出便可: 当n个数的和不为3的倍数时,若n个数中有模3余数与和模3余数相同时,去掉其中最小的,否则去掉两个模3余数不为0且与和模3余数不同的数中最小的.若没有这些数,输出-1: 注意当结果为0时,不能输出前导0: ps:开始写了一个很复杂的代码,后来看过

Hometask

Hometask codeforces 155C 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 consistin

CodeForces 154A Hometask dp

题目链接: http://codeforces.com/problemset/problem/154/A 题意: 给你一个字符串,和若干模板串(长度为2),至少删除多少个字母,使得字符串的字串里面没有模板串. 题解: dp[i][j]表示合法子串s[0...i]的最后一位为j时的最小花费(花费指删除的字母个数) 对每一位考虑删和不删的情况: 如果删,那么它的最后一位一定为之前出现过的. 如果不删,那么s[i]就是最后一位. 考虑这两种情况的转移就可以了. 时间复杂度:O(n*26*26) 代码:

Codeforces Round #260 (Div. 2) B

Description Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression: (1n + 2n + 3n + 4n) mod 5 for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e

Codeforces Round #260 (Div. 2) B. Fedya and Maths(循环节)

题目链接:http://codeforces.com/problemset/problem/456/B B. Fedya and Maths time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Fedya studies in a gymnasium. Fedya's maths hometask is to calculate t

CodeForces - 404A(模拟题)

Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the Engli