Crisp String CodeForces - 1117F (状压)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head
#ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m, mx, ans = INF;
char s[N];
int a[N], tmp[1<<17], f[1<<17];
int cst[1<<17], dp[1<<17];
int g[17][17]; void dfs(int x) {
if (tmp[x]) return;
tmp[x] = 1;
for (int y=x; y; y^=y&-y) dfs(x^y&-y);
}
void dfs1(int x) {
if (f[x]||tmp[x]) return;
tmp[x] = 1;
ans = min(ans, cst[x]);
for (int y=x; y; y^=y&-y) dfs1(x^y&-y);
} int main() {
scanf("%d%d%s", &n, &m, s+1), mx=(1<<m)-1;
REP(i,1,n) ++cst[1<<(a[i]=s[i]-'a')];
REP(i,1,mx) cst[i]=cst[i^i&-i]+cst[i&-i];
REP(i,0,m-1) REP(j,0,m-1) scanf("%d", &g[i][j]);
REP(i,0,m-1) REP(j,0,m-1) if (!g[i][j]) {
int now = 0, pre = -1;
REP(k,1,n) {
if (a[k]==i||a[k]==j) {
if (a[k]==i&&pre==j||a[k]==j&&pre==i) {
dfs(mx^now);
}
now = 0, pre = a[k];
}
else now |= 1<<a[k];
}
int t = 1<<i|1<<j;
REP(k,0,mx) {
if ((k&t)==t) f[k] |= tmp[k];
tmp[k]=0;
}
}
dfs1(mx);
printf("%d\n", ans);
}
Crisp String CodeForces - 1117F (状压)的更多相关文章
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- Vladik and cards CodeForces - 743E (状压)
大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...
- Keyboard Purchase CodeForces - 1238E (状压)
大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小. ...
- CodeForces 11D(状压DP 求图中环的个数)
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...
- Clear The Matrix CodeForces - 903F (状压)
大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...
- Pollywog CodeForces - 917C (状压)
链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...
- Codeforces 678E 状压DP
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
- Codeforces 8C 状压DP
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
随机推荐
- Win7 搭建Linux开发环境
Vargant Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境.它使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境. 功能特 ...
- linux之tail和head的使用
tail 基本介绍 用于显示文件的结尾的内容.在默认情况下,taild命令显示文件的后10行内容 表达式 tail [options] [filenames] 常用参数 -c:输出最后N个字节 -f: ...
- 列表 & 元组& join & range
一:列表(增删改查,列表的嵌套,列表的循环) 1)增加 append (在列表的尾部增加) insert (插入) insert(插入的位置,插入的内容) extend ...
- jquery实现一个标签图标hover到上面的时候显示tooltip
设计图: 解决思路:1.在thumbnailbox.js这个插件中加入tags弹出框显示的内容,一开始让这些内容display:none; 然后再用css画出来一个三角形 实现方法: 知识点:Jque ...
- (转)git 忽略规则
对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是".gitignor ...
- [js]js设计模式-构造函数模式
构造函数模式 function WriteJsPerson(name,age) { this.name=name; //不用手动创建obj this.age = age; this.writeJs=f ...
- 如果merge分支出现问题,使用git方式查看日志
Administrator@IT-20161115IKEG MINGW32 ~$ cd e: Administrator@IT-20161115IKEG MINGW32 /e$ ls$RECYCLE. ...
- python使用grpc调用rpc接口
proto文件: syntax = "proto3"; package coupon; // //message UnsetUseC2URequest { // int64 bid ...
- SpringBoot项目启动时链接数据库很慢
SpringBoot项目启动时链接数据库很慢 springboot项目在启动时候,如下图所示,链接数据库很慢 解决方法:在mysql 的配置文件中 配置 skip-name-resolve
- 去掉vim的BELL提示音
title: date: 2017-11-09 15:07:08 tags: vim categories: 开发工具 --- 在vi/vim中使用 :set noeb 意思是noerrorbells ...