PKU_campus_2018_D Chocolate
思路:
题目链接http://poj.openjudge.cn/practice/C18D/
kruskal过程中使用乘法原理计数。
实现:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = , INF = 0x3f3f3f3f;
const ll MOD = 1e9 + ;
string s[MAXN];
int d[MAXN][MAXN];
int par[MAXN];
ll num[MAXN];
struct edge
{
int a, b, l;
};
edge es[MAXN * MAXN];
void init(int n)
{
for (int i = ; i < n; i++) { par[i] = i; num[i] = ; }
}
int find(int x)
{
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
int uni(int x, int y)
{
x = find(x); y = find(y);
if (x == y) return x;
par[x] = y; return y;
}
int edit_dis(string a, string b)
{
int la = a.length(), lb = b.length();
for (int i = ; i <= la; i++) d[i][] = i;
for (int i = ; i <= lb; i++) d[][i] = i;
for (int i = ; i <= la; i++)
{
for (int j = ; j <= lb; j++)
{
if (a[i - ] == b[j - ]) d[i][j] = d[i - ][j - ];
else d[i][j] = min(d[i][j - ], d[i - ][j]) + ;
}
}
return d[la][lb];
}
bool cmp(edge & x, edge & y)
{
return x.l < y.l;
}
bool check(int x, int cnt)
{
int minn = INF, maxn = -INF;
for (int i = ; i < cnt; i++)
{
int px = find(es[i].a), py = find(es[i].b);
if (px == x && py == x) maxn = max(maxn, es[i].l);
else if ((px == x && py != x) || (px != x && py == x))
minn = min(minn, es[i].l);
}
return maxn < minn;
}
int main()
{
int t, n;
cin >> t;
while (t--)
{
cin >> n;
init(n);
for (int i = ; i < n; i++) cin >> s[i];
int cnt = ;
for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++)
{
es[cnt].a = i; es[cnt].b = j; es[cnt].l = edit_dis(s[i], s[j]);
cnt++;
}
}
sort(es, es + cnt, cmp);
ll ans = ; int tot = n;
for (int i = ; i < cnt; i++)
{
int px = find(es[i].a), py = find(es[i].b);
if (px == py) continue;
int tmp = uni(es[i].a, es[i].b);
num[tmp] = num[px] * num[py] % MOD;
if (check(tmp, cnt)) num[tmp] = (num[tmp] + ) % MOD;
tot--;
if (tot == ) ans = num[tmp];
}
cout << ans << endl;
}
return ;
}
PKU_campus_2018_D Chocolate的更多相关文章
- Big Chocolate
Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
- hdu----(4301)Divide Chocolate(状态打表)
多校综合排名前25名的学校请发送邮件到HDUACM@QQ.COM,告知转账信息(支付宝或者卡号) Divide Chocolate Time Limit: 2000/1000 MS (Java/Oth ...
- Codeforces Round #340 (Div. 2) B. Chocolate 水题
B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...
- Codeforces Round #310 (Div. 1) C. Case of Chocolate set
C. Case of Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/ ...
- codeforces 678C C. Joty and Chocolate(水题)
题目链接: C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input s ...
- CodeForces 689C Mike and Chocolate Thieves (二分+数论)
Mike and Chocolate Thieves 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/G Description ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...
随机推荐
- Java 通过 HTTP 下载文件
1. [代码]Download.java package core.spider; import java.io.*;import java.net.*;import java.util.*; / ...
- iOS中NSNotification、delegate、KVO三者之间的区别与联系?
前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有个疑问,他们的功能比较类似,那么在实际的编程中,如何选择这些方式呢? 在网上看到一个博客上详细 ...
- [SHOI 2009] 会场预约
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2028 [算法] 直接用std :: set维护即可 时间复杂度 : O(NlogN) ...
- [TJOI2012]防御
https://www.zybuluo.com/ysner/note/1332539 题面 戳我 解析 一道挺棒棒的线段树. 显然一次伤害到来时我们要先看看区间内哪些点的护甲没了. 这个可以通过维护区 ...
- SPOJ FIBOSUM && FIBOSUM2
Fibonacci数列定义为 $$f_n = f_{n-1}+f_{n-2}, \text{以及初值}f_0=0, f_1=1.$$ 本文之讨论,皆在模$10^9+7$意义下. FIBOSUM 给定$ ...
- 关于数据库优化2——关于表的连接顺序,和where子句的前后顺序,是否会影响到sql的执行效率问题
有好多时候,我们常听别人说大表在前,小表在后,包括现在好多百度出来的靠前的答案都有说数据库是从右到左加载的,所以from语句最后关联的那张表会先被处理.如果三表交叉,就选择交叉表来作为基础表.等等一些 ...
- 从mysql高可用架构看高可用架构设计
高可用HA(High Availability)是分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计减少系统不能提供服务的时间. 假设系统一直能够提供服务,我们说系统的可用性是100%.如果 ...
- 弹射起步~django
sudo apt-get installl tree 开始创建工作目录 django-admin.py startproject mysite 创建一个名为 mysite的子目录,然后我们通过tree ...
- $P2872\ [USACO07DEC]道路建设Building\ Roads$
\(problem\) 错的原因是\(RE\)(大雾 , 时刻谨记 \(N\) 个地方的话 保守开 \(\frac{N^2}{2}\) 大小. 因为是边. 边最多的情况即完全图 : $1+2+3+4. ...
- 通过split命令分割大文件
场景 线上出了问题,我需要去查找log来定位问题,但是由于线上数据量庞大,这些log文件每过一个小时就会自动回滚一次,尽管如此,有的log文件依然达到了五六g以上的大小. 对于这种巨大的log文件,常 ...