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);
}

原文地址:https://www.cnblogs.com/uid001/p/10754403.html

时间: 2024-08-30 16:06:10

Crisp String CodeForces - 1117F (状压)的相关文章

hdu 4909 String (map + 状压)

题目大意: 给定一个可能含'?'的字符串.然后问这个字符串有多少个子串是含有所有的字符都只出现两次. 其中'?' 可以被替换成任意字符,也可以被remove... 思路分析: 这是bestcoder的round #3的第三题. 这道题的做法和 4908 的做法差不多. 我们把 '?' 左右两边的状态分别处理出来. 然后用map 计数.然后枚举左边的状态.同时枚举? 对应的字符. 然后去寻找右边对应的状态,此时 ans 就可以加上答案. 注意的是要考虑到以 ? 结尾的情况.所以在 ? 的状态要和上

Keyboard Purchase CodeForces - 1238E (状压)

大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小. 状压$dp$, 费用提前计算一下, 预处理$cost_{i,j}$表示与字符$i$相连的状态为$j$时的方案数 总复杂度是$O(n 2^n)$ #include <iostream> #include <sstream> #include <algorithm> #inc

codeforces 580d 状压DP

题意:有n种菜,现在选m种菜来吃,如果在吃y的前一道菜是x的话,那么就可以获得额外满意度.每一种菜都有一个满意度. 思路:设dp[i][S]表示为最后一道菜为i,现在的菜吃的状态为S.S中的二进位如果为1表示已经吃了,如果是0则表示没吃,状压DP,答案就出了. #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm>

codeforces 482c 状压+概率DP

题意:给出N个不同的串,长度一样,别人随机选一个串,你要询问他那个串某一个位置是什么字符直到能确定那个串才能停止,问询问次数的期望. 题解:50个串20个位置容易想到状压,把字符串长度状压先考虑能否在某一个状态确定哪些字符串能确定哪些不能确定,需要2^m*m次,然后时间上不能再乘以n不然会爆,想想只要我知道到达某一个猜位置状态的概率dp[i],再知道相对应有哪些字符串可以确定和不可以确定,用f[i]来表示,那么对于不能确定的字符串相当于就要再猜一步,那么加上这个状态的概率就行了,不会再需要乘以n

Codeforces 8C 状压DP

题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不能暂时放在地上.问最小的花费是多少?花费是笛卡尔距离的平方. 思路一看n 只有24,应该很容易想到要用状压DP. 那么dp[i]表示i状态并且回到原点的最小花费.那么就暴力枚举拿1个或两个物品放回原点,然后转移就行了.需注意,这个题目中拿物品的顺序对答案无影响,比如先拿1号,再拿2号和先拿2号,再拿1号的

Codeforces - 71E 状压DP

参考官方题解 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rrep(i,j,k) for(register int i=j;i>=k;i--) using namespace std; string s[100]={"H","He","Li","Be","B", &

Hongcow Buys a Deck of Cards CodeForces - 744C (状压)

大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑卡数, 求买完所有卡最少天数. 这题挺巧妙的, 刚开始看花费的范围太大一直在想怎么贪心... 实际上注意到减费最多只有120, 可以按照减费进行dp即可 这题CF大神的最优解写了个模拟退火ORZ #include <iostream> #include <algorithm> #inc

2017-03-18 HDU 5733 计算几何 codeforces 599E 状压dp(待补)

HDU 5733 题意:给出四面体的四个顶点,求出其内切球的球心坐标和半径,如果不存在内切球,输出"O O O O". tags:一堆公式..可以做模板了 我们可以将平面上的四点得到由同一个点出发的三个矢量.这样就可以计算这三个矢量的混合积M,则M/6即为四面体体积V. 题目无解的情况当且仅当四点共面,即混合积为0. 求得四面体体积后,可以根据公式r = 3V/(S1+S2+S3+S4)得到内切球半径, S1~S4为四面体四个面的面积. 当前的问题转化为如何求四面体四个面的面积. 由于

codeforces 1185G1 状压dp

include include include include include include include include include include include include include include include using namespace std; typedef long long LL; typedef long long ll; typedef pair<LL, LL> pLL; typedef pair<LL, int> pLi; typed