FJNU Fang G and his Friends(状压DP)题解
Description
Fang G
发现,每个队伍能发挥出的能力值和这个队伍之间满意值是相等的,而一个队伍之间满意值定义为每一个人对于这个队伍所有人(注意是所有人哦!!!)满意度总和的总和,而一个人对另外一个人的满意度和他们的名字是有关的,A对B的满意度定义为A名字前缀和B名字后缀的最长公共前缀(其实就是首x尾o相x连o辣)。
而Fang G要做的就是如何分为两队,使得ooxx更加精彩!但是Fang G 现在有事情要做(是什么事情呢?),没办法完成分配工作,你现在就要帮他完成这个伟大的使命!使得两队在能力值差值最小的情况下,能力值较低的那一队满意度尽量高。
Input
接下来每组测试数据里:
第一行输入一个n代表待分队的人数(1<=n<=20)
接下来有n行,每行有一个字符串si代表每个人的姓名0<|si|<=10000
Output
Sample Input
1
3
aab
bbc
cca
Sample Output
3 7
思路:KMP能求出互相之间的满意度。然后我们要遍历所有可能。遍历的复杂度O(2 ^ n),但是如果我们用n * n的复杂度去判断每一种情况每队的满意度会超时。我们可以用状压DP。显然一共1 << n种可能。我们从0开始往1 << n - 1判,最低位出现的1一定是新变化的,那么我就可以直接算出原来的情况+新增一个最低位的1,达到O(n)算出每队的满意度。
代码:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = + ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
char s[][maxn];
int w[][];
int Next[maxn], len[maxn];
int dp[ << ];
int n;
void preKMP(int pos){
int i = , j = -;
Next[] = -;
while(i < len[pos]){
while(j != - && s[pos][i] != s[pos][j]) j = Next[j];
++i;++j;
Next[i] = j;
}
}
int KMP(int p, int str){
int i = , j = ;
while(i < len[str]){
while(j != - && s[p][j] != s[str][i]) j = Next[j];
++j;
++i;
if(i == len[str]) return j;
if(j >= len[p]){
j = Next[j];
}
}
return ;
}
int dis, ans1, ans2;
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d" ,&n);
for(int i = ; i < n; i++){
scanf("%s", s[i]);
len[i] = strlen(s[i]);
}
for(int i = ; i < n; i++){
preKMP(i);
for(int j = ; j < n; j++){
if(j == i){
w[i][j] = len[i];
continue;
}
w[i][j] = KMP(i, j);
}
} memset(dp, , sizeof(dp));
int mx = ( << n) - ;
for(int i = ; i <= mx; i++){
for(int j = ; j < n; j++){
if(i & ( << j)){
for(int k = j + ; k < n; k++){
if(i & ( << k)){
dp[i] += w[j][k] + w[k][j];
}
}
dp[i] += dp[i - ( << j)] + w[j][j];
break;
}
}
}
dis = INF;
ans1 = -INF, ans2 = -INF;
for(int i = ; i <= mx; i++){
if(abs(dp[i] - dp[mx ^ i]) < dis){
dis = abs(dp[i] - dp[mx ^ i]);
ans1 = min(dp[i], dp[mx ^ i]);
ans2 = max(dp[i], dp[mx ^ i]);
}
else if(abs(dp[i] - dp[mx ^ i]) == dis){
int r1 = min(dp[i], dp[mx ^ i]);
int r2 = max(dp[i], dp[mx ^ i]);
if(r1 > ans1){
ans1 = r2, ans2 = r2;
}
}
}
printf("%d %d\n", ans1, ans2);
}
return ;
}
FJNU Fang G and his Friends(状压DP)题解的更多相关文章
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- CF453B Little Pony and Harmony Chest (状压DP)
CF453B CF454D Codeforces Round #259 (Div. 2) D Codeforces Round #259 (Div. 1) B D. Little Pony and H ...
- fzu2188 状压dp
G - Simple String Problem Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- loj 1316(spfa预处理+状压dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27024 题意:求0-(n-1)的经过最多的标记的点的最短路. 思路 ...
- HDU5816 Hearthstone(状压DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collec ...
- HDU 4568 Hunter 最短路+状压DP
题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...
随机推荐
- Hint: Fallback method 'public java.lang.String queryUserByIdFallback(java.lang.Long)' must return: User or its subclass
1.错误日志 熔断器添加错误方法返回时,报了一个 error. com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionExc ...
- zabbix基础服务搭建
监控系统的介绍 性能优化和监控点 1.cpu 调度器 有效的分配cpu的时间片 上下文切换 运行队列 ...
- jQuery-AutoComplete自动提示简单实现
注:本次案列实现功能为 用户注册信息,如果数据库对应表中存在部分信息,点击已有的用户的用户名,自动补全其它已有的基本信息 实现思路:通过AutoComplete提示,异步通过用户名查询全表,充当Aut ...
- FPC全制造组装的流程介绍(转载)
[维文信FPC]FPC又称柔性电路板,FPC的PCBA组装焊接流程与硬性电路板的组装有很大的不同,因为FPC板子的硬度不够,较柔软,如果不使用专用载板,就无法完成固定和传输,也就无法完成印刷.贴片.过 ...
- 【Mac】-NO.161.Mac.1 -【MacOS Error running 'Tomcat 8.5.371': Cannot run program Permission denied】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- openpyxl.utils.exceptions.IllegalCharacterError
如果是手动调用 xlwt 这种第三方库除了错可能没法找错误,但是从错误中我们看到错误是由 openpyxl 抛出的,我们试着从 openpyxl 中找解决方案 出错处的代码 value = value ...
- Spring Cloud微服务架构图
- Unable to find a constructor to use for type System.Security.Claims.Claim. A class should either have a default constructor
Newtonsoft.Json DeserializeObject 反序列化 IdentityServer4.Models Cliecnt 错误: Newtonsoft.Json.JsonSeria ...
- 分享一个不错的Unittest测试报告
分享一个不错的,unittest测试报告. 先上图: 代码如下: 复制下来保存成py可用 #coding=utf-8 """ A TestRunner for use w ...
- Pocket Gems面经prepare: Diamond and Ruby
说我有一个背包,有n个格子,一个格子可以放5个钻石,一个钻石10块钱,一个格子可以放5个ruby,一个ruby 5块钱, 一个格子可以放一个装备,一个装备25块钱. 然后给你n个钻石n个ruby n个 ...