hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]
题目链接:https://hihocoder.com/problemset/problem/1334
描述
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]的更多相关文章
- HihoCoder第九周 状态压缩 二 与POJ2411总结
在此我向各位博友求助,特别想知道除了HihoCoder上面的结果要对1e9+7取余之外,这两道题还有什么其他的问题,都是骨牌覆盖问题,都是状态压缩+dp,为什么我能过poj2411的程序过不了Hiho ...
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point
// 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...
- hihoCoder week8 状态压缩·一
状态压缩 写了两个半小时 太菜了 题目链接 https://hihocoder.com/contest/hiho8/problem/1 #include <bits/stdc++.h> ...
- 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II
http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...
- hihocoder 1331 - 扩展二进制数 - [hiho一下168周]
题目链接:http://hihocoder.com/problemset/problem/1331 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们都知道二进制数的每 ...
- hihocoder 1330 - 数组重排 - [hiho一下167周][最小公倍数]
题目链接:https://hihocoder.com/problemset/problem/1330 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi想知道,如果他 ...
- hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]
题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...
- hiho一下第134周 1468 : 2-SAT·hihoCoder新春晚会
1468 : 2-SAT·hihoCoder新春晚会 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 hihoCoder新春晚会正在紧张地筹备中.晚会分为上半场和下半场, ...
随机推荐
- C# winform开发嵌套Chrome内核浏览器(WebKit.net)开发(一)
https://www.cnblogs.com/Maxq/p/6566558.html WebKit.net是对WebKit的.Net封装, 使用它.net程序可以非常方便的集成和使用webkit作为 ...
- PMP用语集
AC actual cost 实际成本 ACWP actual cost of work performed 已完工作实际成本 BAC budget at completion 完工预算 BCWP b ...
- 手把手让你实现开源企业级web高并发解决方案(lvs+heartbeat+varnish+nginx+eAccelerator+memcached)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeze.blog.51cto.com/1846439/677348 此文凝聚 ...
- VMware 14 的永久许可密钥
VMware workstation 14永久激活密钥分享: CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3WK-AFXEK-488JP-A7MQX-XL8YF AC5XK-0ZD ...
- [转载]使用PHP模拟HTTP认证
[转载]使用PHP模拟HTTP认证 如果你希望在每个脚本的基础上实现口令保护功能,那么你可以通过结合header()函数和$PHP_AUTH_USER.$PHP_AUTH_PW全局变量的方法来创建一个 ...
- C# - 获取类中属性的名称
用反射控制的,不过获取属性名称的方法,用方法形式获取的,不知道消耗大不大 using System; using System.Collections.Generic; using System.Li ...
- kubernetes 测试 Mariadb gtid 主从复制.
k8s 为 1个master 3个node 下载镜像 : mariadb 镜像版本是10.2.13 (此时10.3还没发布正式版) docker pull mariadb push到私有仓库 dock ...
- Material Design系列第六篇——Defining Custom Animations
Defining Custom Animations //自定义动画 This lesson teaches you to //本节课知识点 Customize Touch Feedback //1. ...
- 一、K3 Cloud 开发插件《K3 Cloud开发手册》
1.K3 Cloud打开 提示错误:未能加载文件或程序集“XXXXXXXXX”或它的某一个依赖项 原因:未正常关机导致的Temporary ASP.NET Files文件夹下的文件损坏 解决:删除C: ...
- ASP.NET MVC View使用Conditional compilation symbols
由于View(.cshtml)的运行时编译关系,在项目级别中定义的symbols是无法被直接使用的.需要在Web.config中添加compilerOptions(在View目录下的Web.confi ...