POJ.3087 Shuffle’m Up (模拟)

题意分析

给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12。

将字符串s1和s2通过一定的变换变成s12,找到变换次数

变换规则如下:

假设s1=12345,s2=67890

变换后的序列 s=6172839405

如果s和s12完全相等那么输出变换次数

如果不完全相等,s的前半部分作为s1,后半部分作为s2,重复上述过程。

直接模拟,注意给出的顺序是从底到上的。

代码总览

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#define nmax 205
using namespace std;
char str1[nmax],str2[nmax],str[nmax];
int t,ans,n;
void change(char temp[])
{
int pos = 0;
for(int i = 0;i<n;++i){
temp[pos++] = str2[i];
temp[pos++] = str1[i];
}
temp[2*n] = '\0';
ans++;
}
void split(char temp[])
{
int i;
for(i = 0;i<n;++i)
str1[i] = temp[i];
str1[i] = '\0';
for(int j = 0;j<n;++j,++i)
str2[j] = temp[i];
str2[i] = '\0';
}
void bfs()
{
char temp[nmax];
memset(temp,0,sizeof(temp));
change(temp);
set<string> s;
s.clear();
while(1){
if(strcmp(temp,str) == 0) return;
if(s.count(temp) == 1){
ans = -1;
return;
}else{
s.insert(temp);
}
split(temp);
change(temp);
}
}
int main()
{
scanf("%d",&t);
for(int i = 1; i<=t;++i){
ans = 0;
scanf("%d",&n);
scanf("%s",str1);
scanf("%s",str2);
scanf("%s",str);
bfs();
printf("%d %d\n",i,ans);
} return 0;
}

最新文章

  1. 转载:ActiveMQ的可靠性机制
  2. 第三章 对象(JavaScript:语言精粹)
  3. Emmet语法大全手册
  4. IIS7显示ASP的详细错误信息到浏览器
  5. 经典算法系列--kmp
  6. Core Bluetooth Programming Guide
  7. 【转】Cocos2d-x 2.x CCSprite 灰白图的生成(利用shader设置)&mdash;&mdash;2013-08-27 21
  8. Stanford CoreNLP--Split Sentence
  9. Linux shell (一)
  10. 分享到QQ空间、新浪微博、腾讯微博的代码!(收藏)
  11. Render和template?
  12. 2.PHP 教程_PHP 安装
  13. 使用jQuery实现tag便签去重效果
  14. Flex 弹性盒模型
  15. android动画特效之解决解决移动后闪烁现象,解决输入法弹出后位置回复原状,解决两个动画叠加
  16. VUE2.0实现购物车和地址选配功能学习第五节
  17. LeetCode4. Median of Two Sorted Arrays---vector实现O(log(m+n)--- findkth
  18. IScroll.js 学习笔记
  19. mysql 5.7~默认sql_mode解读
  20. GD32 ------ 使用外部中断,中断函数需要延时才能读到真正电平

热门文章

  1. 软考之信息安全工程师(包含2016-2018历年真题详解+官方指定教程+VIP视频教程)
  2. 英特尔&#174; 实感™ 摄像头 (F200) 应用如何实现最佳用户体验
  3. 常用JDBC数据库驱动包和类名
  4. mysql中latin1编码中文转utf8
  5. Scrum立会报告+燃尽图(Beta阶段第二周第五次)
  6. scrum立会报告+燃尽图(第三周第三次)
  7. 20162328蔡文琛 week06 大二
  8. Android开发第二阶段(1)
  9. Ubuntu环境下No module named &#39;_tkinter&#39;错误的解决
  10. centos快速安装lamp