标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510
普通集合会tle,换高贵的并查集。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 const int maxn = 510; 23 const int maxm = 2020; 24 int n, ans; 25 int len[maxn]; 26 char s[maxn][maxm]; 27 set<int> rep[maxn]; 28 29 inline bool scan_d(int &num) { 30 char in;bool IsN=false; 31 in=getchar(); 32 if(in==EOF) return false; 33 while(in!=‘-‘&&(in<‘0‘||in>‘9‘)) in=getchar(); 34 if(in==‘-‘){ IsN=true;num=0;} 35 else num=in-‘0‘; 36 while(in=getchar(),in>=‘0‘&&in<=‘9‘){ 37 num*=10,num+=in-‘0‘; 38 } 39 if(IsN) num=-num; 40 return true; 41 } 42 43 int main() { 44 // freopen("in", "r", stdin); 45 int T, _ = 1; 46 scan_d(T); 47 while(T--) { 48 ans = 0; 49 memset(s, 0, sizeof(s)); 50 memset(len, 0, sizeof(len)); 51 scan_d(n); 52 for(int i = 1; i <= n; i++) { 53 scanf("%s", s[i]); 54 rep[i].clear(); 55 } 56 int ans = -1; 57 for(int i = 1; i <= n; i++) { 58 for(int j = i - 1; j >= 1; j--) { 59 if(len[i] < len[j]) continue; 60 if(rep[i].find(j) != rep[i].end()) continue; 61 if(strstr(s[i], s[j]) != NULL) { 62 rep[i].insert(j); 63 } 64 else { 65 ans = i; 66 break; 67 } 68 } 69 } 70 printf("Case #%d: %d\n", _++, ans); 71 } 72 return 0; 73 }
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 const int maxn = 510; 23 const int maxm = 2020; 24 int n, ans; 25 int pre[maxn]; 26 int len[maxn]; 27 char s[maxn][maxm]; 28 29 int find(int x) { 30 return x == pre[x] ? x : pre[x] = find(pre[x]); 31 } 32 bool check(int x, int y) { 33 x = find(x); 34 y = find(y); 35 if(x == y) return 1; 36 return 0; 37 } 38 39 int main() { 40 // freopen("in", "r", stdin); 41 int T, _ = 1; 42 scanf("%d", &T); 43 while(T--) { 44 ans = 0; 45 memset(s, 0, sizeof(s)); 46 memset(len, 0, sizeof(len)); 47 scanf("%d", &n); 48 for(int i = 1; i <= n; i++) { 49 scanf("%s", s[i]); 50 } 51 for(int i = 1; i <= n; i++) { 52 pre[i] = i; 53 } 54 int ans = -1; 55 for(int i = 1; i <= n; i++) { 56 for(int j = i - 1; j >= 1; j--) { 57 if(len[i] < len[j]) continue; 58 if(check(i, j)) continue; 59 if(strstr(s[i], s[j]) != NULL) { 60 pre[i] = j; 61 } 62 else { 63 ans = i; 64 break; 65 } 66 } 67 } 68 printf("Case #%d: %d\n", _++, ans); 69 } 70 return 0; 71 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/5438948.html