zoukankan      html  css  js  c++  java
  • hihocoder 第170周 Word Construction (dfs+剪枝)

    #1334 : Word Construction

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

    描述

    Given N words from the top 100 common words in English (see below for reference), select as many words as possible as long as no two words share common letters.

    Assume that the top 100 common words in English are:

    the be to of and a in that have i it for not on with he as you do at this but his by from they we say her she or an will my one all would there their what so up out if about who get which go me when make can like time no just him know take people into year your good some could them see other than then now look only come its over think also back after use two how our work first well even new want because any these give day most us

    输入

    The first line contains an integer N, denoting the number of words. (1 ≤ N ≤ 40)  

    The second line contains N words from the top 100 common words.

    输出

    Output the most number of words can be selected.

    样例输入
    8
    the be to of and a in that 
    样例输出
    4

    题意:

    给出N个单词(1 ≤ N ≤ 40),在满足各个单词之间没有重复字母的情况下,求能选取最多多少个单词。

    题解:

    dfs+剪枝,因为要求字母不重复

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int N = 40 + 10;
    int a[200] = {0}, vis[N] = {0};
    string s[N];
    int ans = 0, n;
    int dfs(int num)
    {
        int tmp = 0;
        vis[num] = 1;
        for(int i = 0; i < n; i++) {
            if(vis[i])
                continue;
            int len = s[i].length();
            //出现重复字母
            for(int j = 0; j < len; j++) {
                if(a[s[i][j]])
                    len = -1;
            }
            if(len == -1)
                continue;
            for(int j = 0; j < len; j++) {
                a[s[i][j]]++;
            }
            tmp = max(tmp, dfs(i));
            for(int j = 0; j < len; j++) {
                a[s[i][j]]--;
            }
        }
        vis[num] = 0;
        return tmp + 1;
    }
    int  main()
    {
        cin >> n;
        for(int i = 0; i < n; i++) {
            cin >> s[i];
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            memset(vis, 0, sizeof(vis));
            memset(a, 0, sizeof(a));
            for(int j = 0; j < s[i].length(); j++)
                a[s[i][j]]++;
            ans = max(ans, dfs(i));
        }
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    WSDL
    对协程的理解
    调用webServer
    待看
    BZOJ4668 冷战(并查集)
    BZOJ4651 NOI2016网格(割点)
    Lyft Level 5 Challenge 2018
    BZOJ3073 PA2011Journeys(线段树+bfs)
    BZOJ4602 SDOI2016齿轮(搜索)
    BZOJ4597 SHOI2016随机序列(线段树)
  • 原文地址:https://www.cnblogs.com/yu0111/p/7635747.html
Copyright © 2011-2022 走看看