http://poj.org/problem?id=3087

注意复制字符串的时候,要在末尾加上'\0',否则导致strcmp出错。

还有就是开数组大小的时候看清楚一点,别开错了debug了好久。

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
using namespace std;
int t, n, k, kase=;
char s12[], s1[], s2[];
char vis[][];
typedef struct{
int cnt;
char s[];
}Node;
int panduan(Node a)
{
if(!strcmp(s12, a.s))
return ;
return ;
}
int panduan2(Node a)
{
for(int i = ; i < k; i++){
if(!strcmp(a.s, vis[i])){
return ;
}
}
return ;
}
void bfs()
{
k=;
int flag=;
Node node;
queue<Node> q;
int j = ;
for(int i = ; i < *n; i += ){
node.s[i] = s2[j++];
}
j = ;
for(int i = ; i < *n; i += ){
node.s[i] = s1[j++];
}
node.s[*n] = '\0';
node.cnt=;
q.push(node);
while(!q.empty()){
Node t = q.front(), p;
strcpy(vis[k++], t.s);
q.pop();
/*for(int i = 0; i < 2*n; i++){
cout << t.s[i];
} cout << endl;*/
if(panduan(t)){
flag=;
cout << ++kase << " " << t.cnt << endl;
break;
}
//拆分
j=;
for(int i = ; i < n; i++){
s1[j++] = t.s[i];
}
j=;
for(int i = n; i < *n; i++){
s2[j++] = t.s[i];
}
//合并
j=;
for(int i = ; i < *n; i += ){
p.s[i] = s2[j++];
}
j = ;
for(int i = ; i < *n; i += ){
p.s[i] = s1[j++];
}
p.s[*n] = '\0';
p.cnt=t.cnt+;
if(panduan2(p)){
//strcpy(vis[k++], p.s);
q.push(p);
}
}
if(!flag){
cout << ++kase << " " << "-1" << endl;
}
}
int main()
{
cin >> t;
while(t--){
cin >> n;
cin >> s1 >> s2 >> s12;
memset(vis, , sizeof(vis));
//cout << ++kase << " ";
bfs();
}
return ;
}

最新文章

  1. Flat UI 工具包
  2. SQL详解(下)
  3. 博创arm板编译内核makefile不兼容问题解决
  4. Extjs jar包问题
  5. IDEA操作GIT说明
  6. 【c++内存分布系列】单继承
  7. Hbase深入学习(六) Java操作HBase
  8. MySQL数据库的存储结构
  9. string与数值之间的转换
  10. Android如何在Framework层使用解锁代码
  11. Apache 2.x+jboss6.1反向代理session共享问题设置
  12. asp.net弹出层实例
  13. Myeclipse Java项目转换成Maven项目
  14. common js CMD/AMD 是什么 和他们之间的联系区别
  15. 树莓派超声波测距+蜂鸣器(c语言)
  16. 解决记录:win10 无法安装VS2017,visual studio installer下载进度始终为0
  17. SSM-MyBatis-15:Mybatis中关联查询(多表操作)
  18. React-代码规范
  19. 49-Python 安装pythoncom库和pyHook
  20. windows平台mysql密码破解设置

热门文章

  1. 去除ArrayList集合中的重复自定义对象元素
  2. linux服务器查看tcp链接shell
  3. 【Android】Bitmap加载图片错误 java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  4. 【Android】让Python在Android系统上飞一会儿
  5. gcc make 与cmake
  6. git之二: git可视化工具sourcetree
  7. BZOJ2480 Spoj3105 Mod 数论 扩展BSGS
  8. python面试题之python下多线程的限制
  9. ImportError: No module named &#39;pysqlite2&#39;
  10. 【LeetCode算法-14】Longest Common Prefix