#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 (状压)的更多相关文章

  1. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  2. Vladik and cards CodeForces - 743E (状压)

    大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...

  3. FZU - 2218 Simple String Problem(状压dp)

    Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...

  4. Keyboard Purchase CodeForces - 1238E (状压)

    大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小. ...

  5. CodeForces 11D(状压DP 求图中环的个数)

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...

  6. Clear The Matrix CodeForces - 903F (状压)

    大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...

  7. Pollywog CodeForces - 917C (状压)

    链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...

  8. Codeforces 678E 状压DP

    题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...

  9. Codeforces 8C 状压DP

    题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...

随机推荐

  1. Ubuntu系统启动后停在(initramfs)

    问题 今天我在启动虚拟机过程 遇到莫名其妙的问题,启动不了.如下图.提示某个文件系统错误了.例如我的就是 /dev/mapper/vagrant--vg-root . 上面问题 可把我急坏了,以为虚拟 ...

  2. python 中的__new__与__init__

    在Python中的class中有两个方法__new__与__init__,有什么区别呢? class TestCls(): """docstring for TestCl ...

  3. 《nginx - 基本操作/配置》

    一:基本操作 - 开启 Nginx nginx -c nginx.conf -  Nginx 的平滑重启 kill -HUP nginx主进程号(平滑重启) -  停止 Nginx * Kill -Q ...

  4. python自动类型转换(针对于Number数据类型来的)精度从低到高 bool->int-> float->complex 当两个不同是数据类型运算时候,默认想更高进度转化

    # ### 自动类型转换(针对于Number数据类型来的) ''' 精度从低到高 bool->int-> float->complex 当两个不同是数据类型运算时候,默认想更高进度转 ...

  5. gitlab的ssh key有2个

    Gitlab添加SSH key可以pull不能push的问题 最后解决的是 使用http去clone pull  提交 没用ssh.就是需要输入密码

  6. Unity之显示fps功能

    如下: using UnityEngine; using System.Collections; public class ShowFpsOnGUI : MonoBehaviour { public ...

  7. Fluxion无线攻击

    使用步骤 github地址 https://github.com/deltaxflux/fluxion 进入到fluxion目录下 ./fluxion 启动fluxion  启动之后会先检测没有安装的 ...

  8. tensorboard窥视

    运行神经网络时,跟踪网络参数,以及输入输出是很重要的,可据此判断模型是否在学习,损失函数的值是否在不断减小.Tensorboard通过可视化方法,用于分析和调试网络模型. 使用tensorboard的 ...

  9. Delphi中类的运行期TypeInfo信息结构说明(转载)

    Delphi中类的运行期TypeInfo信息结构说明作者:刘啸CnPack开发组 http://www.cnpack.org关键字:RTTI, TypeInfo, TypeData, PropInfo ...

  10. Beautiful Soup 学习手册

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式   快速开始 下面的一段HTML代码将作为例 ...