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

      

  • 相关阅读:
    apache配置
    windows 查看端口号,杀进程
    c/c++ 拷贝控制 右值与const引用
    c/c++ 多线程 多个线程等待同一个线程的一次性事件
    c/c++ 多线程 等待一次性事件 异常处理
    c/c++ 多线程 等待一次性事件 std::promise用法
    c/c++ 多线程 等待一次性事件 packaged_task用法
    c/c++ 多线程 等待一次性事件 future概念
    c/c++ 多线程 利用条件变量实现线程安全的队列
    c/c++ 多线程 一个线程等待某种事件发生
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/6383039.html
Copyright © 2011-2022 走看看