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个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
随机推荐
- Java学习-049-正则工具类
自去年九月份决定再次入学和职业资格进阶,开始备战二者考试至今,以及当下进行中的职称申请,犹如孤独的狼,不断前行在路上,而今凡凡总总的已历8月... 不感慨了,如下为一园友需要的正则工具类,直接上码: ...
- mongodb 3.2 分片 + 副本集
从图中可以看到有四个组件:mongos.config server.shard.replica set. mongos,数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加 ...
- Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader sun.misc.Launcher$AppClassLoader@18b4aac2
1.错误日志 Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader sun.mis ...
- SharePoint 命令行
网站集备份: Backup-SPSite http://sp2013 -Path C:\sp.bak 网站集还原: Restore-SPSite http://sp2013/sites/dyzx -P ...
- 016-并发编程-java.util.concurrent.locks之-Lock及ReentrantLock
一.概述 重入锁ReentrantLock,就是支持重进入的锁 ,它表示该锁能够支持一个线程对资源的重复加锁.支持公平性与非公平性选择,默认为非公平. 以下梳理ReentrantLock.作为依赖于A ...
- 【UML】NO.46.EBook.5.UML.1.006-【UML 大战需求分析】- 用例图(Use Case Diagram)
1.0.0 Summary Tittle:[UML]NO.46.EBook.1.UML.1.006-[UML 大战需求分析]- 用例图(Use Case Diagram) Style:DesignPa ...
- 家庭记账本之微信小程序(六)
Wxss的学习 WXSS(WeiXin Style Sheets)是一套样式语言,用于描述WXML的组件样式. WXSS用来决定WXML的组件应该怎么显示. 为了适应广大的前端开发者,我们的WXSS具 ...
- 2.jQuery介绍
. jQuery的认识 jQuery的核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口:具有高效灵活的css选择器,并且可对CSS选择器进行扩展:拥有便捷的插件扩展机制和丰富的插件. 和原 ...
- python操作wifi
python连接wifi需要使用pywifi包,可以用pip install pywifi安装 1.导入包 import pywifi,time #保存包中写义的常量 from pywifi impo ...
- C# AsyncCallback异步回调用法示例
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...