SPOJ AMR12E Dyslexic Gollum
2013-10-16 18:08
260 查看
AC自动机+DP。题目要求是生成一个长度为n的 0-1 串,其中最长回文子串的长度小于 k ( 1 <= k <= 10)。因为k长度只有10,所以我们可以先预处理出长度小于11的所有回文串,然后利用找出的回文串建立AC自动机,然后生成一个长度为n的满足题意的串就可以了。AC自动机中节点信息保存为到达该点最长的回文串长度,这样就可以保证利用自动机产生子回文串小于 k 的串了,因为一旦到达长度大于k的节点我们就不向下更新就可以了。至于为什么我们建立自动机的时候只找长度小于11的,明显这是因为我们需要的最长的回文子串是9嘛,而为了让生成的串长度不大于9,我们需要防止回文串由9直接加两个变成11,这样就不满足题意了。。。所以需要判断下一步会不会产生长度11的串。。。这题有非AC自动机的DP做法,时空效率都很高,同学们可以去搜一下。。。
#include <algorithm> #include <iostream> #include <sstream> #include <cstdlib> #include <climits> #include <cstring> #include <cstdio> #include <string> #include <vector> #include <cctype> #include <queue> #include <cmath> #include <set> #include <map> #define CLR(a, b) memset(a, b, sizeof(a)) using namespace std; const int MAX_NODE = 2000 * 2; const int INF = 0x3f3f3f3f; const int MOD = 1000000007; const int CHILD_NUM = 2; const int N = 11; class ACAutomaton { private: int chd[MAX_NODE][CHILD_NUM]; int fail[MAX_NODE]; int val[MAX_NODE]; int Q[MAX_NODE]; int ID[128]; int sz; public: void Initialize() { fail[0] = 0; ID['0'] = 0; ID['1'] = 1; } void Reset() { CLR(chd[0] , 0);sz = 1; CLR(val, 0); } void Insert(char *a, int sit) { int p = 0; for ( ; *a ; a ++) { int c = ID[*a]; if (!chd[p][c]) { CLR(chd[sz] , 0); chd[p][c] = sz ++; } p = chd[p][c]; } val[p] = sit; } void Construct() { int *s = Q , *e = Q; for (int i = 0 ; i < CHILD_NUM ; i ++) { if (chd[0][i]) { fail[ chd[0][i] ] = 0; *e ++ = chd[0][i]; } } while (s != e) { int u = *s++; for (int i = 0 ; i < CHILD_NUM ; i ++) { int &v = chd[u][i]; if (v) { *e ++ = v; fail[v] = chd[fail[u]][i]; val[v] = max(val[v], val[fail[v]]); } else { v = chd[fail[u]][i]; } } } } int dp[2][MAX_NODE][12]; int Work(int n, int k) { CLR(dp, 0); dp[0][0][0] = 1; for(int i = 0; i < n; i ++) { CLR(dp[(i + 1) & 1], 0); for(int j = 0; j < sz; j ++) { if(val[j] < k && (val[chd[j][0]] < k || val[chd[j][1]] < k)) for(int s = 0; s < k; s ++) { if(!dp[i & 1][j][s]) continue; int c = chd[j][0], tp; tp = max(val[c], s); if(tp < k)dp[(i + 1) & 1][c][tp] = (dp[(i + 1) & 1][c][tp] + dp[i & 1][j][s]) % MOD; c = chd[j][1]; tp = max(val[c], s); if(tp < k)dp[(i + 1) & 1][c][tp] = (dp[(i + 1) & 1][c][tp] + dp[i & 1][j][s]) % MOD; } } }int ret = 0; for(int i = 1; i < sz; i ++) { for(int j = 0; j < k; j ++) { ret = (ret + dp[n & 1][i][j]) % MOD; } } return ret; } } AC; char ch1[N + 1], ch2[N + 1]; int ok(int x) { int a , ret = 0; while(x) { if(x & 1) a[ret ++] = 1; else a[ret ++] = 0; x >>= 1; } for(int i = 0; i <= ret / 2; i ++) { if(a[i] != a[ret - 1 - i]) return 0; } return ret; } int main() { int t, n, k;AC.Initialize();AC.Reset(); for(int i = 1; i < (1 << N); i ++) { int s = ok(i), ret; ret = s; if(s) { int x = i;ch2[s] = ch1[s] = '\0'; while(x) { if(x & 1) ch1[-- s] = '1', ch2[s] = '0'; else ch1[-- s] = '0', ch2[s] = '1'; x >>= 1; } AC.Insert(ch1, ret);AC.Insert(ch2, ret); } }AC.Construct(); scanf("%d", &t); while(t --) { scanf("%d%d", &n, &k); printf("%d\n", AC.Work(n, k)); } }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- HDU4758 AC自动机+DP (HDU4758与HDU2222)
- [LeetCode] Distinct Subsequences
- [LeetCode] Interleaving String
- [LeetCode] Unique Binary Search Tree
- [LeetCode] Edit Distance
- [LeetCode] Minimum Path Sum
- [LeetCode] Unique Paths
- [LeetCode] Unique Paths II
- [LeetCode] Maximum Subarray
- [LeetCode] Trapping Rain Water
- [LeetCode] Binary Tree Maximum Path Sum
- [LeetCode] Best Time to Buy and Sell Stock III
- [LeetCode] Scramble String
- [LeetCode] Longest Palindromic Substring
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- Leetcode 91. Decode Ways JAVA语言