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;
    }
    

      

  • 相关阅读:
    Hbase 安装
    Hive操作
    Hive安装
    HDFS操作
    hadoop安装
    番茄时间管理法(Pomodoro Technique)
    测试架构师修炼之道:“秘书九段”的故事
    windows远程连接报:身份错误,函数不支持的解决办法
    Linux crontab配置
    Hadoop 历史服务配置启动查看
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/6383039.html
Copyright © 2011-2022 走看看