[HDOJ5510]Bazinga(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510
普通集合会tle,换高贵的并查集。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
const int maxm = ;
int n, ans;
int len[maxn];
char s[maxn][maxm];
set<int> rep[maxn]; inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
scan_d(T);
while(T--) {
ans = ;
memset(s, , sizeof(s));
memset(len, , sizeof(len));
scan_d(n);
for(int i = ; i <= n; i++) {
scanf("%s", s[i]);
rep[i].clear();
}
int ans = -;
for(int i = ; i <= n; i++) {
for(int j = i - ; j >= ; j--) {
if(len[i] < len[j]) continue;
if(rep[i].find(j) != rep[i].end()) continue;
if(strstr(s[i], s[j]) != NULL) {
rep[i].insert(j);
}
else {
ans = i;
break;
}
}
}
printf("Case #%d: %d\n", _++, ans);
}
return ;
}
TLE
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
const int maxm = ;
int n, ans;
int pre[maxn];
int len[maxn];
char s[maxn][maxm]; int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
bool check(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return ;
return ;
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
scanf("%d", &T);
while(T--) {
ans = ;
memset(s, , sizeof(s));
memset(len, , sizeof(len));
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%s", s[i]);
}
for(int i = ; i <= n; i++) {
pre[i] = i;
}
int ans = -;
for(int i = ; i <= n; i++) {
for(int j = i - ; j >= ; j--) {
if(len[i] < len[j]) continue;
if(check(i, j)) continue;
if(strstr(s[i], s[j]) != NULL) {
pre[i] = j;
}
else {
ans = i;
break;
}
}
}
printf("Case #%d: %d\n", _++, ans);
}
return ;
}
[HDOJ5510]Bazinga(并查集)的更多相关文章
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- Matlab画柱状和折线对照图
上面是效果图,看着很不错吧,主要的问题在于用XTickLabel设置横坐标时候,横坐标会扩展,就是说如果label是[1 2 3],咱就做了三组试验,参数分别是 1 2 3,但是显示是1 2 ...
- SQL Server 锁表说明
锁定数据库的一个表 SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOCK) 其 ...
- StoreKit framework
关于In-APP Purchase 1. 中文文档 基本流程: http://www.cnblogs.com/wudan7/p/3621763.html 三种购买形式及StoreKit code介绍: ...
- Create Script Template In Edit Mode
很多时候 许多类 的 格式 都是重复的,比如 从配置文件中映射出来的类. 这个时候写一个 类模板 就很节省时间了. Code public static string TestPath = " ...
- 解决Flash和html在多标签浏览器中互访问题
在Flash播放器运行时,将不同来源的资源划分到独立的沙箱(sandbox)内,不同沙箱之间不能 彼此操作数据(除非目标沙箱做过一些设置,授权其他沙箱可访问),这就是Flash的跨沙箱问题.当Flas ...
- Using the viewport meta tag to control layout on mobile browsers
A typical mobile-optimized site contains something like the following: <meta name="viewport& ...
- Ruby中的Profiling工具
看看如何调试Ruby的性能问题 李哲 - APRIL 08, 2015 Ruby内置的profiler 内置的profiler实现的很简单,在ruby2.2中只有150行代码,大家可以看看它的实现pr ...
- Web前端名词释义及原理
引言:看题目的时候,不要觉得这是一个很深奥的问题,Web前端这些东西很多就是叫的名字牛逼,其实原理很TM简单,也就那么回事. 一.javascript名词释义 1.啥是事件队列? 就是 弄一个数组,里 ...
- Visual Studio 2015支持为Linux构建应用
点这里 微软著名的集成开发环境有可能是首次在其产品页提及了竞争对手Linux.Visual Studio 2015的页面声称,“Build for iOS, Android, Windows devi ...
- POJ 2533 Longest Ordered Subsequence
题目描述:LIS(Longest Increasing Subsequence)模板题 分析:O(n^2)的方法 状态表示:d[i]表示以i结尾的最长上升子序列长度 转移方程:d[i]=max{ 1, ...