思路:

题目链接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的更多相关文章

  1. Big Chocolate

    Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...

  2. Dividing a Chocolate(zoj 2705)

    Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...

  3. hdu----(4301)Divide Chocolate(状态打表)

    多校综合排名前25名的学校请发送邮件到HDUACM@QQ.COM,告知转账信息(支付宝或者卡号) Divide Chocolate Time Limit: 2000/1000 MS (Java/Oth ...

  4. Codeforces Round #340 (Div. 2) B. Chocolate 水题

    B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...

  5. 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/ ...

  6. codeforces 678C C. Joty and Chocolate(水题)

    题目链接: C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. CodeForces 689C Mike and Chocolate Thieves (二分+数论)

    Mike and Chocolate Thieves 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/G Description ...

  8. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

  9. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate

    UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...

随机推荐

  1. oracle问题系列 : ORA-02290: 违反检查约束条件

    报错如下: ### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-022 ...

  2. css3中我们不知道的一些属性

    1.图片作为边框:border-image; 2.圆角问题:border-radius:上.下.左.右: 3.字体的阴影与自动换行: 阴影: h1 {text-shadow: 5px 5px 5px ...

  3. 关于animate的一些属性

    animate() 方法执行 CSS 属性集的自定义动画.该方法通过CSS样式将元素从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 " ...

  4. HDU2181 哈密顿绕行世界问题 —— DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...

  5. Java中的Lock

    同步机制是Java并发编程中最重要的机制之一,锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能防止多个线程同时访问共享资源(但是也有例外,比如读写锁).Java中可以使用synchroniz ...

  6. JQuery树形插件Dynatree的包装对象

    这是JQuery Dynatree插件的包装对象,做了些改进和增强,增加了右键菜单,以及相应事件等扩展1. [代码]MagicDTree的基本使用 <SCRIPT type=text/javas ...

  7. Oracle:通过dbv查看数据文件是否有坏块

    我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...

  8. 西交校赛 F. GZP and Poker

    F. GZP and Poker GZP often plays games with his friends.Today they went to a board game.There are n ...

  9. 用mkdirs创建目录

    import java.io.*; class a { public static void main(String args[]) { createDir("c:/fuck"); ...

  10. 微服务框架go-micro

    微服务框架go-micro https://www.cnblogs.com/li-peng/p/9558421.html 产品嘴里的一个小项目,从立项到开发上线,随着时间和需求的不断激增,会越来越复杂 ...