思路:

题目链接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. Spring Boot 整合Filter

    两种方法 方法一: 正常创建好Filter类,配置完成 package clc.user.filter; import javax.servlet.Filter; import javax.servl ...

  2. Hadoop 中的 (side data) 边数据

    一.用途 边数据是作业所需的额外的只读数据,通常用来辅助主数据集: 二.方法 1.利用Configuration类来配置,利用setter()和getter()可方便的使用,方便存储一些基本的类型: ...

  3. Windows下VMware虚拟机使用Centos,Docker方式安装openstf的小坑

    今天使用docker方式安装openstf碰到了一小坑,坑了我半天.特此记录! docker方式安装stf就不说了,网上教程一大把. 但是... 安装完之后.进入web控制界面,手机连接的好好的.但硕 ...

  4. HTTP服务器用什么组件或者方式比较好

    我目前用Indy的HttpServer组件来编写,但遇到一个暂时没有办法解决的问题,就是上传文件到这个HTTPServer,如果文件名包含中文,则会出现乱码.网上查了一下,这是个indy的遗留问题,据 ...

  5. HDU2089 不要62 —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...

  6. nginx开发_调试日志

    接口列表 核心文件ngx_log.h 主要接口如下: ngx_log_error(level, log, err, fmt, ...) ngx_log_debug(level, log, err, f ...

  7. 「SHOI2007」「Codevs2341」 善意的投票(最小割

    2341 善意的投票 2007年省队选拔赛上海市队选拔赛 时间限制: 5 s 空间限制: 128000 KB 题目等级 : 大师 Master   题目描述 Description 幼儿园里有n个小朋 ...

  8. codevs1258关路灯

    传送门 1258 关路灯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每天早晨他 ...

  9. WebDev.WebServer40.EXE命令使用

    "C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE&quo ...

  10. "Activity" 总结

    1.什么是Activity? 1.四大组件之一 2.通常一个界面对应一个activity 3.是Context的子类 4.同时实现window.callback和keyevent.callback回调 ...