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

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

     题解1:dp[i]表示到位置i能保留下来的字符串的最长长度,在s[i]和s[j]不冲突的情况下,dp[i]=max(dp[i],dp[j]+1);但是这样下去会超时,又因为输入只包含字符,所以设tmp[s[i]]表示以s[i]结尾的dp最大值,则dp[i]=max(dp[i],tmp[s[i]]+1)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn=1e5+10;
     7 int dp[maxn];
     8 int vis[30][30];
     9 int tmp[30];
    10 char s[maxn];
    11 char op[5];
    12 int len;
    13 int k;
    14 int main()
    15 {
    16     while(~scanf("%s",s+1))
    17     {
    18         len=strlen(s+1);
    19         scanf("%d",&k);
    20         memset(tmp,0,sizeof(tmp));
    21         memset(dp,0,sizeof(dp));
    22         memset(vis,0,sizeof(vis));
    23         while(k--)
    24         {
    25             scanf("%s",op);
    26             vis[op[0]-'a'][op[1]-'a']=1;
    27             vis[op[1]-'a'][op[0]-'a']=1;
    28         }
    29         dp[1]=1;
    30         tmp[s[1]-'a']=1;
    31         int ans=1; 
    32         for(int i=2;i<=len;i++)
    33         {
    34             for(int j=0;j<26;j++)
    35             {
    36                 if(!vis[s[i]-'a'][j])
    37                 {
    38                     dp[i]=max(dp[i],tmp[j]+1);
    39                 }
    40             }
    41             tmp[s[i]-'a']=max(tmp[s[i]-'a'],dp[i]);
    42             ans=max(ans,dp[i]);
    43         }    
    44         //for(int i=1;i<=len;i++)
    45         //    printf("%d ",dp[i]);
    46         printf("%d
    ",len-ans );
    47     }
    48 }

    题解2:思维

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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 #define maxn 100005
     7 char ch[maxn];
     8 char s[2];
     9 int n,k;
    10 int main()
    11 {
    12     scanf("%s",ch);
    13     int len=strlen(ch);
    14     scanf("%d",&k);    
    15         int i,ans=0;
    16     while(k--)
    17     {
    18         getchar();
    19         scanf("%s",s);
    20         for(i=0;i<len;i++)
    21         {
    22             int l=0,r=0;
    23             while(ch[i]==s[0]||ch[i]==s[1])
    24             {
    25                 if(ch[i]==s[0]) l++;
    26                 if(ch[i]==s[1]) r++;
    27                 i++;
    28             }
    29             ans+=min(l,r);
    30         }
    31     }
    32     printf("%d
    ",ans);
    33     return 0;
    34 }
  • 相关阅读:
    spring FactoryBean配置Bean
    注意使用 BTREE 复合索引各字段的 ASC/DESC 以优化 order by 查询效率
    Mysql经常使用基本命令汇总及默认账户权限与改动
    图像边缘检測--OpenCV之cvCanny函数
    HDU 1556 Color the ball 树状数组 题解
    JMeter使用记录2 -- Web測试
    C++编程
    矩阵树定理速证
    DM816x算法具体解释--之OSD
    哥尼斯堡的“七桥问题”(25分)(欧拉回路,并查集)
  • 原文地址:https://www.cnblogs.com/1013star/p/9744455.html
Copyright © 2011-2022 走看看