题意:有一些小孩(至少两个)围成一圈,有 n 轮游戏,每一轮从某个小孩开始往左或者往右伟手帕,拿到手帕写上自己的性别(B,G),然后以后相同方向给下一个。
然后在某个小孩结束,给出 n 轮手帕上的序列,求最少有多少个小孩。
析:很容易知道是状压DP,也很容易写出状态方程,dp[s][i][j] 表示 已经选择了 s 的手帕,然后第一个是 i,最后一个是 j,然后再转移,很简单,但是,
这样时间复杂度可能是 n^4*2^n ,太大了,所以必须减少一维状态,dp[s][i] 时间复杂度是 n^2*2^n,可以接受,表 已经选择了 s 的手帕,然后最后一个是 i,
并且规定第 1 个序列是最正向的在最前面。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 10000 + 10; const int mod = 100000000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int dp[1<<16][16][2]; string str[16][2]; int overlap[16][16][2][2]; int solve(const string &a, const string &b){ for(int i = 1; i < a.size(); ++i){ if(i + b.size() <= a.size()) continue; bool ok = true; for(int j = i; j < a.size(); ++j) if(a[j] != b[j-i]){ ok = false; break; } if(ok) return a.size() - i; } return 0; } int main(){ ios_base::sync_with_stdio(false); while(cin >> n && n){ for(int i = 0; i < n; ++i){ cin >> str[i][0]; str[i][1] = str[i][0]; reverse(str[i][1].begin(), str[i][1].end()); } vector<string> v[2]; for(int i = 0; i < n; ++i){ bool ok = true; for(int j = 0; j < n; ++j){ if(i == j) continue; if(str[j][0].size() < str[i][0].size()) continue; if(str[j][0].find(str[i][0]) != string::npos || str[j][1].find(str[i][0]) != string::npos){ ok = false; break; } } if(ok) v[0].push_back(str[i][0]), v[1].push_back(str[i][1]); } if(v[0].empty()) v[0].push_back(str[0][0]), v[1].push_back(str[0][1]); n = v[0].size(); int all = 1<<n; for(int i = 0; i < n; ++i) for(int x = 0; x < 2; ++x) for(int j = 0; j < n; ++j) for(int y = 0; y < 2; ++y) overlap[i][j][x][y] = solve(v[x][i], v[y][j]); memset(dp, INF, sizeof dp); dp[1][0][0] = v[0][0].size(); --all; for(int i = 1; i < all; ++i) for(int j = 0; j < n; ++j) for(int x = 0; x < 2; ++x){ if(dp[i][j][x] == INF) continue; for(int k = 1; k < n; ++k){ if(i&(1<<k)) continue; for(int y = 0; y < 2; ++y) dp[i|(1<<k)][k][y] = min(dp[i|(1<<k)][k][y], dp[i][j][x] + (int)v[0][k].size() - overlap[j][k][x][y]); } } int ans = INF; for(int i = 0; i < n; ++i) for(int x = 0; x < 2; ++x){ if(dp[all][i][x] == INF) continue; ans = min(ans, dp[all][i][x] - overlap[i][0][x][0]); } if(1 == ans) ans = 2; cout << ans << endl; } return 0; }