您的位置:首页 > 其它

HDU-4628 Pieces 搜索 | DP

2013-08-02 21:13 218 查看
  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628


  数据不大,枚举本质。首先对枚举出回文串,然后用DP或者搜索,这里因为层数不多,用bfs比较好,或者用IDA*。。。

//STATUS:C++_AC_140MS_780KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=(1<<16)+10;
const int INF=0x3f3f3f3f;
const int MOD=100000,STA=8000010;
const LL LNF=1LL<<60;
const double EPS=1e-8;
const double OO=1e15;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End

char s[18];
int sta
,d
,t[18];
int T,len,k,up;

bool check(int status)
{
int i,j;
for(i=j=0;status;i++,status>>=1)
if(status&1)t[j++]=i;
for(i=0;i<(j>>1);i++)
if(s[t[i]]!=s[t[j-i-1]])return false;
return true;
}

void getsta()
{
int i;
up=(1<<(len=strlen(s)))-1;
k=0;
for(i=1;i<=up;i++)
if(check(i))sta[k++]=i;
}

int bfs()
{
int i,u,v;
queue<int> q;
q.push(0);
mem(d,-1);d[0]=0;
while(!q.empty()){
u=q.front();q.pop();
for(i=0;i<k;i++){
v=sta[i];
if((u&v)==0 && d[u|v]==-1){
d[u|v]=d[u]+1;
q.push(u|v);
if((u|v)==up)return d[u|v];
}
}
}
return -1;
}

int main()
{
//  freopen("in.txt","r",stdin);
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
getsta();
printf("%d\n",bfs());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: