思路:

题目链接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. Unable to resolve target android-5解决方案

    1:问题:android导入项目的时候出现此错误 2:原因: 3:解决: 修改工程目录下的default.properties文件里的内容target=android-5 这个5修改成你的api版本就 ...

  2. HDU2102 A计划 —— BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others)    Me ...

  3. VMware Ubuntu 共享文件夹

    /**************************************************************************** * VMware Ubuntu 共享文件夹 ...

  4. SPOJ:NO GCD (求集合&秒啊)

    You are given N(1<=N<=100000) integers. Each integer is square free(meaning it has no divisor ...

  5. 【USACO】 奶牛政坛

    [题目链接] 点击打开链接 [算法] tarjan算法求LCA [代码] #include<bits/stdc++.h> #define MAXN 200010 #pragma GOC o ...

  6. Watir: 在使用test/unit的时候要注意,不需要require的时候别require

    假设我书写了很多测试用例,测试用例中都有:require 'test/unit' 后来我想把很多这样的测试用例组织在一起运行,我使用了两个require: require 'test/unit' re ...

  7. cocos2d-js使用plist执行自身动作

    首先需要将精灵动作帧动画图片使用TexturePacker创建plist,创建好后,将生成的plist和png图片(所有帧动画图片集成的一张大图): 百牛信息技术bainiu.ltd整理发布于博客园 ...

  8. iOS View

    创建: 2018/04/19 完成: 2018/04/20 View的创建  创建  storyboard上操作  与代码连接 ● 目的: 通过代码控制view ● 按住option拖动 View的坐 ...

  9. 洛谷 - P1141 - 01迷宫 - dfs

    https://www.luogu.org/problemnew/show/P1141 能互相到达的格子的答案自然是一样的,第一次dfs标记联通块,第二次dfs把cnt传递到整个联通卡并顺手消除vis ...

  10. bzoj 1396: 识别子串 && bzoj 2865: 字符串识别【后缀数组+线段树】

    根据height数组的定义,和当前后缀串i最长的相同串的长度就是max(height[i],height[i+1]),这个后缀贡献的最短不同串长度就是len=max(height[i],height[ ...