[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距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- Leetcode#91 Decode Ways
原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...
- mysql解决错误的方法-MySQL日志
1.使用ps -ef|grep mysql查询是否有与MySQL相关的僵尸进程,如果有则强制杀掉 2.在配置文件my.cnf中配置启动错误日志: log_error = /var/log/mysql/ ...
- 重启nginx后丢失nginx.pid解决
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- [百度空间] [转]关于Direct3D多窗口编程的一篇翻译
Introduction In DirectX 8, support for rendering to multiple windows is provided through the creatio ...
- JS通过ajax动态读取xml文件内容
http://www.sharejs.com/codes/javascript/8178 HTML文件代码如下 <!DOCTYPE html> <html> <head& ...
- 添加hive默认配置hiverc
可以在$HOME中加一个.hiverc文件,并在里面配置hive启动的一些参数. Fro example: http://hadooped.blogspot.com/2013/08/hive-hive ...
- SQL分页查询总结{转}
开发过程中经常遇到分页的需求,今天在此总结一下吧.简单说来方法有两种,一种在源上控制,一种在端上控制.源上控制把分页逻辑放在SQL层:端上控制一次性获取所有数据,把分页逻辑放在UI上(如GridVie ...
- Sqli-labs less 40
Less-40 本关的sql语句为SELECT * FROM users WHERE id=('$id') LIMIT 0,1 我们根据sql语句构造以下的payload: http://127.0. ...
- asp.net 认证与授权
1.下面的例子在web.config文件中配置网站使用asp.net forms 身份认证方式: <configuration> <system.web> <authen ...
- SGU 105
There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. ...