Bazinga(HDU5510+KMP)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bazinga(HDU5510+KMP)相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ons' title='ons'>ons   href   style   ref   mat   ext   break   syn   print   

t棰樼洰閾炬帴锛?a href="http://acm.hdu.edu.cn/showproblem.php?pid=5510" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5510

棰樼洰锛?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

棰樻剰锛氭壘鍒颁竴涓紪鍙锋渶澶х殑瀛楃涓叉弧瓒筹細瀛樺湪涓€涓紪鍙锋瘮瀹冨皬鐨勫瓧绗︿覆涓嶆槸瀹冪殑瀛椾覆銆?/p>

鎬濊矾锛欿MP銆備絾鏄繖棰樼殑澶嶆潅搴﹀ぇ鑷翠负1e8锛屾澀鐢垫湇鍔″櫒璺戠ǔT锛屾垜杩樿瘯浜嗕竴鍙?_-||銆傛兂浜嗗緢涔呮兂鍒颁竴涓巹瀛︿紭鍖栵紝鎴戜滑棣栧厛姣旇緝鐩搁偦鐨勪袱涓瓧绗︿覆锛屽亣璁句负i鍜宨-1锛屼笖i-1鏄痠鐨勫瓧涓诧紝閭d箞濡傛灉鏌愪釜澶х紪鍙峰寘鍚玦鍒欏繀鐒跺寘鍚玦-1锛屾鏃跺氨娌℃湁蹇呰鍐嶅拰i-1璺戜竴杈筀MP浜嗐€傚涓嬪浘鎵€绀猴細

鎶€鏈垎浜浘鐗? src=

浠g爜瀹炵幇濡備笅锛?/p>

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <stack>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 typedef long long ll;
17 typedef pair<ll, ll> pll;
18 typedef pair<ll, int> pli;
19 typedef pair<int, ll> pil;;
20 typedef pair<int, int> pii;
21 typedef unsigned long long ull;
22 
23 #define lson i<<1
24 #define rson i<<1|1
25 #define bug printf("*********
");
26 #define FIN freopen("D://code//in.txt", "r", stdin);
27 #define debug(x) cout<<"["<<x<<"]" <<endl;
28 #define IO ios::sync_with_stdio(false),cin.tie(0);
29 
30 const double eps = 1e-8;
31 const int mod = 10007;
32 const int maxn = 2000 + 7;
33 const double pi = acos(-1);
34 const int inf = 0x3f3f3f3f;
35 const ll INF = 0x3f3f3f3f3f3f3f;
36 
37 int t, n;
38 int nex[507][maxn], len[507], vis[507];
39 char s[507][maxn];
40 
41 void get_next(int x) {
42     nex[x][1] = 0;
43     for(int i = 2, j = 0; i <= len[i]; i++) {
44         while(j > 0 && s[x][i] != s[x][j+1]) j = nex[x][j];
45         if(s[x][i] == s[x][j+1]) j++;
46         nex[x][i] = j;
47     }
48 }
49 
50 bool kmp(int x, int y) {
51     for(int i = 1, j = 0; i <= len[x]; i++) {
52         while(j > 0 && (j == len[y] || s[x][i] != s[y][j+1])) j = nex[y][j];
53         if(s[x][i] == s[y][j+1]) j++;
54         if(j == len[y]) {
55             return true;
56         }
57     }
58     return false;
59 }
60 
61 int main() {
62     //FIN;
63     scanf("%d", &t);
64     int icase = 0;
65     while(t--) {
66         scanf("%d", &n);
67         memset(vis, 0, sizeof(vis));
68         for(int i = 1; i <= n; i++) {
69             scanf("%s", s[i] + 1);
70             len[i] = strlen(s[i] + 1);
71             get_next(i);
72         }
73         int flag = 1;
74         for(int i = n; i >= 2; i--) {
75             if(kmp(i, i-1)) {
76                 vis[i-1] = 1;
77             }
78         }
79         for(int i = n; i >= 2; i--) {
80             for(int j = i - 1; j >= 1; j--) {
81                 if(!vis[j] && !kmp(i, j)) {
82                     flag = i;
83                     break;
84                 }
85             }
86             if(flag != 1) break;
87         }
88         printf("Case #%d: ", ++icase);
89         if(flag != 1) printf("%d
", flag);
90         else printf("-1
");
91     }
92     return 0;
93 }

 

以上是关于Bazinga(HDU5510+KMP)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5510 Bazinga(KMP)

Bazinga(HDU5510+KMP)

hdu 5510 Bazinga(字符串kmp)

HDU 5510 Bazinga(kmp)

Bazinga HDU - 5510 不可做的暴力

HDU-5510 Bazinga