#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. Postman 进阶(pre-request scripts&test script)

    Postman 进阶 1. pre-request scripts   pre-request scripts是一个关联了收藏夹内request,并且在发送request之前执行的代码片段.这对于在r ...

  2. angular--解决angular图片加载失败问题

    基于angular4写的一个指令,在ionic3.x项目在用.因为加载图片超时等原因导致图片显示不出来,需要替换成默认或者指定图片 1.err-src.ts import { Directive,In ...

  3. 列表 & 元组& join & range

    一:列表(增删改查,列表的嵌套,列表的循环) 1)增加 append    (在列表的尾部增加) insert       (插入)   insert(插入的位置,插入的内容) extend      ...

  4. Java并发问题--乐观锁与悲观锁以及乐观锁的一种实现方式-CAS

    首先介绍一些乐观锁与悲观锁: 悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁.传统的关系型数据库里边就用到了很 ...

  5. 解决docker镜像pull超时问题

    第一步:通过dig @114.114.114.114 registry-1.docker.io找到可用IP dig @114.114.114.114 registry-1.docker.io 第二步: ...

  6. CentOS6.5安装pip

    首先重要的事情说三遍,因为可能有程序依赖目前的python2环境,比如yum: 不要动现有的python2环境! 不要动现有的python2环境! 不要动现有的python2环境! 如果你动了,yum ...

  7. Gis数据处理

    几何投影和解析投影几何投影是将椭球面上的经纬线网投影到几何平面上,然后将几何面展为平面.几何投影可以分为方位投影.圆柱投影和圆锥投影.这三种投影纬线的形状不同.方位投影纬线的形状是同心圆:圆柱投影纬线 ...

  8. python hello 的三种方法

    1)直接 print("hello") 2)使用main函数: def main(): print("Helloa你好") if __name__ == &qu ...

  9. IDEA循环依赖报错解决方案

    step1.查找循环依赖 step2.在IDEA菜单栏中打开Analyze->Analyze Module Dependencies...看到有的模块被红色的标出来了,此时右边显示了循环依赖,那 ...

  10. Shell 实践、常用脚本

    (1)计算1-100的和. #!/bin/bash n= ` do n=$[$i+$n] done echo $n (2)输一个数字,然后计算出1到数字的和,要求如果输入数字小于1,则重新输入,知道输 ...