思路:

题目链接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. kentico检查当前授权用户,是否为admin角色

    https://docs.kentico.com/k11/custom-development/user-internals-and-api/checking-permissions-using-th ...

  2. Oracle备份与恢复:RMAN

    今天第一次学习RMAN的使用.先登录系统: 数据库未启动,rman命令不能执行.在rman下 也可以 startup . 全库备份的命令:backup  database 查看备份集. 下面模拟,数据 ...

  3. org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误

    当遇到如下错误的时候 可能以为是regionserver 挂掉或者其他原因导致连接不上regionserver  但后面提示了Hbase 表statistic_login 具体信息 Thu Jan 1 ...

  4. Centos Missing Library: QtWebKit.so.4

    /******************************************************************** * Centos Missing Library: QtWe ...

  5. 【CAIOJ1177】 子串是否出现

    [题目链接] 点击打开链接 [算法] KMP [代码] #include<bits/stdc++.h> using namespace std; #define MAXA 1000010 ...

  6. JAVA sleep() & wait()

    对于sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监 ...

  7. python 查找IP地址归属地

    #!/usr/bin/env python # -*- coding: utf-8 -*- #查找IP地址归属地 #writer by keery_log #Create time:2013-10-3 ...

  8. 泛型Class<T>和 T. <T>

    private T product; private Class<T> product; 这两个有什么区别呢,查了资料才知道,单独的T 代表一个类型 而 Class<T>代表这 ...

  9. iView 实战系列教程(21课时)_汇总贴

    iView 实战系列教程(21课时)_汇总贴 课程地址; https://segmentfault.com/ls/1650000016424063 iView 实战系列教程(21课时)_1.iView ...

  10. MFC类别概述

    MFC 类别主要可分为下列数大群组: ■ General Purpose classes - 提供字符串类别.数据处理类别(如数组与串行),异 常情况处理类别.文件类别...等等. ■ Windows ...