zoukankan      html  css  js  c++  java
  • acm专题---动态规划

    题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096

    #1400 : Composition

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Alice writes an English composition with a length of N characters. However, her teacher requires that M illegal pairs of characters cannot be adjacent, and if 'ab' cannot be adjacent, 'ba' cannot be adjacent either.

    In order to meet the requirements, Alice needs to delete some characters.

    Please work out the minimum number of characters that need to be deleted.

    输入

    The first line contains the length of the composition N.

    The second line contains N characters, which make up the composition. Each character belongs to 'a'..'z'.

    The third line contains the number of illegal pairs M.

    Each of the next M lines contains two characters ch1 and ch2,which cannot be adjacent.  

    For 20% of the data: 1 ≤ N ≤ 10

    For 50% of the data: 1 ≤ N ≤ 1000  

    For 100% of the data: 1 ≤ N ≤ 100000, M ≤ 200.

    输出

    One line with an integer indicating the minimum number of characters that need to be deleted.

    样例提示

    Delete 'a' and 'd'.

    样例输入
    5
    abcde
    3
    ac
    ab
    de
    样例输出
    2
    #include <iostream>
    using namespace std;
    #include <vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<math.h>
    #include<iomanip>
    #include<stack>
    int main()
    {
        int n;
        cin>>n;
        string str;
        cin>>str;
        int m;
        cin>>m;
        bool flag[26][26];
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<26;j++)
                flag[i][j]=true;
        }
        for(int i=0;i<m;i++)
        {
            string tmp;
            cin>>tmp;
            flag[tmp[0]-'a'][tmp[1]-'a']=flag[tmp[1]-'a'][tmp[0]-'a']=false;
        }
        int dp[26];
        for(int i=0;i<26;i++)
            dp[i]=0;
        dp[0]=1;
        for(int i=1;i<n;i++)
        {
            int maxnum=0;
            for(int j=0;j<26;j++)
            {
                if(flag[str[i]-'a'][j])
                    maxnum=max(maxnum,dp[j]+1);
            }
            dp[str[i]-'a']=maxnum;
        }
        int ans=0;
        for(int i=0;i<26;i++)
        {
            if(dp[i]>ans)
                ans=dp[i];
        }
        cout<<n-ans<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    c++引用(reference)
    c++ 三目运算符功能增强
    C++ “新增”bool类型关键字
    C++ struct
    C++命名空间(namespace)
    基于python 实现KNN 算法
    Chrome 快捷键使用
    WOE(weight of evidence, 证据权重)
    python 命令运行环境下 ModuleNotFoundError: No module named 'Test'
    基于python 信用卡评分系统 的数据分析
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/6383039.html
Copyright © 2011-2022 走看看