Word Construction

来源:互联网 发布:淘宝网天猫商城女装 编辑:程序博客网 时间:2024/06/05 03:06

题目1 : 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.

样例输入
8the be to of and a in that 
样例输出
4
思路:求最大独立集, 转为求补图的最大团(就是DFS+先验 prune )

https://www.coursera.org/learn/algorithms/lecture/pTihC/072zui-da-tuan-wen-ti

http://blog.csdn.net/whosemario/article/details/8513836

import java.util.Arrays;import java.util.Scanner;/* * 求最大独立集 * 转为求补图的最大团 */public class Main {static int bound = -1;public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();String[] ss = new String[n];for(int i=0; i<n; i++) ss[i] = sc.next();boolean[][] adj = new boolean[n][n];for(int i=0; i<n; i++)Arrays.fill(adj[i], true);for(int i=0; i<n; i++)for(int j=i; j<n; j++)for(char c: ss[j].toCharArray())if(ss[i].indexOf(c) != -1){adj[i][j] = false;adj[j][i] = false;}boolean[] use = new boolean[n];dfs(adj, n, 0, use, 0);System.out.println(bound);}private static void dfs(boolean[][] adj, int n, int s, boolean[] use, int cnt) {bound = Math.max(bound, cnt);if(s == n)return;if(cnt + n-s <= bound)return;boolean canAdd = true;for(int i=0; i<s; i++) {if(use[i] && !adj[i][s]) {canAdd  =false;break;}}if(canAdd) {use[s] = true;dfs(adj, n, s+1, use, cnt+1);use[s] = false;}dfs(adj, n, s+1, use, cnt);}}



原创粉丝点击