码迷,mamicode.com
首页 > 编程语言 > 详细

[hdu4300] next数组的应用

时间:2015-05-22 21:03:34      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

题意:给你一个密文和明文的对应表以及一个密文+明文的字符串,明文可能只出现前面的一部分(也就是说是原明文的前缀),求最短的明文。

思路:首先密文的长度至少占到一半,所以先把那一半解密,问题转化为找一个最长的后缀使得和前缀相等,并且满足后缀长度不超过原串的一半,显然用next数组即可解决。

技术分享
  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 32 #define all(a) (a).begin(), (a).end()
 33 #define lowbit(x) ((x) & (-(x)))
 34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 37 #define pchr(a) putchar(a)
 38 #define pstr(a) printf("%s", a)
 39 #define sstr(a) scanf("%s", a)
 40 #define sint(a) scanf("%d", &a)
 41 #define sint2(a, b) scanf("%d%d", &a, &b)
 42 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 43 #define pint(a) printf("%d\n", a)
 44 #define test_print1(a) cout << "var1 = " << a << endl
 45 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 46 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 47 #define mp(a, b) make_pair(a, b)
 48 #define pb(a) push_back(a)
 49 
 50 typedef unsigned int uint;
 51 typedef long long LL;
 52 typedef pair<int, int> pii;
 53 typedef vector<int> vi;
 54 
 55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 57 const int maxn = 1e3 + 7;
 58 const int md = 1e9 + 9;
 59 const int inf = 1e9 + 7;
 60 const LL inf_L = 1e18 + 7;
 61 const double pi = acos(-1.0);
 62 const double eps = 1e-6;
 63 
 64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 67 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 69 int make_id(int x, int y, int n) { return x * n + y; }
 70 
 71 struct KMP {
 72     int next[300010];
 73     void GetNext(char s[]) {
 74         mem0(next);
 75         next[0] = next[1] = 0;
 76         for(int i = 1; s[i]; i++) {
 77             int j = next[i];
 78             while(j && s[i] != s[j]) j = next[j];
 79             next[i + 1] = s[j] == s[i]? j + 1 : 0;
 80         }
 81     }
 82 };
 83 KMP kmp;
 84 char s[300010], s2[300010], str[600010];
 85 char hsh[300];
 86 int main() {
 87     //freopen("in.txt", "r", stdin);
 88     int T;
 89     cin >> T;
 90     rep_up0(cas, T) {
 91         cin >> s;
 92         int len = strlen(s);
 93         rep_up0(i, len) {
 94             hsh[s[i]] = a + i;
 95         }
 96         cin >> s2;
 97         len = strlen(s2);
 98         int pos = (len + 1) >> 1;
 99         for (int i = 0; i < pos; i ++) s2[i] = hsh[s2[i]];
100         kmp.GetNext(s2);
101         int res = kmp.next[len];
102         while (len - res < pos) res = kmp.next[res];
103         res = len - res;
104         rep_up0(i, pos) str[i] = s[s2[i] - a];
105         for (int i = pos; i < res; i ++) str[i] = s2[i];
106         for (int i = res; i < 2 * res; i ++) str[i] = hsh[str[i % res]];
107         str[2 * res] = 0;
108         puts(str);
109     }
110     return 0;
111 }
View Code

 

[hdu4300] next数组的应用

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4523226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!