Codeforces 155C Hometask【思维+Dp】

351 篇文章 2 订阅

C. Hometask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
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个约束 ,每个约束包含两个字母,表示这两个字母不能在字符串中相邻存在,现在我们需要删除一些字符,使得字符串是合法的。

输出最少删除的字符个数。


思路:


希望删除的最少,反过去想就是希望留下来的最多,那么我们设定Dp【i】表示到位子i,能够留下来的字符串最长长度,那么对应我们能够写出状态转移方程:

Dp【i】=max(Dp【i】,Dp【j】+1),这里需要保证a【j】和a【i】不冲突。

显然直接这样去暴力Dp会超时,我们知道,输入只包含字符,所以我们不如设定temp【i】,表示以字符i结尾的Dp最大值。

那么我们就有:

Dp【i】=max(Dp【i】,temp【j】+1),这里同样需要保证字符j和字符a【i】不冲突才行。


那么:Ans=n-max(Dp【i】)


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char a[150000];
int bid[300][300];
int dp[150000];
int temp[300];
int main()
{
    while(~scanf("%s",a+1))
    {
        int n=strlen(a+1);
        memset(dp,0,sizeof(dp));
        memset(temp,0,sizeof(temp));
        memset(bid,0,sizeof(bid));
        int k;scanf("%d",&k);
        while(k--)
        {
            char b[45];
            scanf("%s",b);
            bid[b[0]][b[1]]=1;
            bid[b[1]][b[0]]=1;
        }
        int output=0;
        for(int i=1;i<=n;i++)
        {
            if(i==1)
            {
                dp[i]=1;
                temp[a[i]]=1;
            }
            else
            {
                for(int j='a';j<='z';j++)
                {
                    if(bid[a[i]][j]==0)
                    {
                        dp[i]=max(dp[i],temp[j]+1);
                    }
                }
                temp[a[i]]=max(temp[a[i]],dp[i]);
            }
            output=max(output,dp[i]);
        }
        printf("%d\n",n-output);
    }
}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值