题目链接:https://hihocoder.com/problemset/problem/1334

时间限制: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),在满足各个单词之间没有重复字母的情况下,求能选取最多多少个单词。

题解:

每个单词总共24个字母,用状态压缩之后,可以表示为小于2^24的整数;

而且能很方便的表示当前哪些字母被占用了,哪些字母还没占用,选某个单词又会占用哪些字母;

并且用过位运算能很方便地进行判断是否有重复字母;

最后,直接DFS即可;

AC代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 43
using namespace std;
int n,word[MAX];
bool vis[MAX];
int dfs(int now,int state)
{
int maxi=;
for(int nex=;nex<=n;nex++)
{
if(vis[nex]) continue;
if(state&word[nex]) continue;
vis[nex]=;
maxi=max(maxi,dfs(nex,state|word[nex]));
vis[nex]=;
}
return now==?maxi:maxi+;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
char str[];
scanf("%s",&str);
word[i]=;
for(int j=;str[j];j++) word[i]|=<<(str[j]-'a');
}
memset(vis,,sizeof(vis));
int ans=dfs(,);
printf("%d\n",ans);
}

hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]的更多相关文章

  1. HihoCoder第九周 状态压缩 二 与POJ2411总结

    在此我向各位博友求助,特别想知道除了HihoCoder上面的结果要对1e9+7取余之外,这两道题还有什么其他的问题,都是骨牌覆盖问题,都是状态压缩+dp,为什么我能过poj2411的程序过不了Hiho ...

  2. hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)

    来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快.   hi ...

  3. 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

    // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...

  4. hihoCoder week8 状态压缩·一

    状态压缩  写了两个半小时  太菜了 题目链接 https://hihocoder.com/contest/hiho8/problem/1 #include <bits/stdc++.h> ...

  5. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  6. hihocoder 1331 - 扩展二进制数 - [hiho一下168周]

    题目链接:http://hihocoder.com/problemset/problem/1331 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们都知道二进制数的每 ...

  7. hihocoder 1330 - 数组重排 - [hiho一下167周][最小公倍数]

    题目链接:https://hihocoder.com/problemset/problem/1330 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi想知道,如果他 ...

  8. hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]

    题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...

  9. hiho一下第134周 1468 : 2-SAT·hihoCoder新春晚会

    1468 : 2-SAT·hihoCoder新春晚会 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 hihoCoder新春晚会正在紧张地筹备中.晚会分为上半场和下半场, ...

随机推荐

  1. Java按钮控件数组实现计算器界面

    编写程序,通过按钮数组来管理界面中的所有按钮控件,从而使用最少的代码实现模拟的计算器界面. 思路如下: 创建一个类,通过extends使其继承窗体类JFrame: 创建一个JFrame对象,使用JFr ...

  2. Spring @Lazy

    @DependsOn用于强制初始化其他Bean.可以修饰Bean类或方法,使用该Annotation时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean. @DependsOn ...

  3. 浅谈iPhone OS(iOS)架构

    iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设备的操作系统

  4. 屏蔽win10中文输入法

    Windows 10自带的默认输入法,是通过Shift键来切换中/英文,这样为玩一些不需要打字,却需要使用Shift键的游戏带来了不便,比如,在进行游戏的时候,按下Shift键后,再按跳跃.射击等键, ...

  5. 03python条件判断与缩进

    if...else...来实现 知识点: 1.多个流程使用elif xxxx : 2.if elif else的语句执行顺序从上而下,只要条件满足,if语句就结束了. 3. 缩进:其他语言用{}包含语 ...

  6. 运行RF测试案例,显示unable to open socket to "localhost:56505" error: [Errno 10061] 错误,且关闭RF卡死的解决办法

    问题描述: 执行WEB ui测试案例后,执行请他的测试案例显示unable to open socket to "localhost:56505" error: [Errno 10 ...

  7. java.util.concurrent.RejectedExecutionException 线程池饱和

    java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPoli ...

  8. Django 添加应用

    一个项目可以添加多个应用,可以使用以下两种方法来添加应用: [root@localhost web]$ python manage.py startapp blog [root@localhost w ...

  9. 第7章 Ajax数据爬取

    Ajax 简介 Ajax 分析方法 Ajax 结果提取

  10. Tiny6410 关于制作ubifs文件系统映像

    Tiny6410的superboot设计使得我不能用yaffs2文件系统,不过也罢,费点事就费点事吧 在这之前要安装mktools系列工具哦~~ 进入工作目录/opt/FriendlyARM/mini ...