POJ.3087 Shuffle'm Up (模拟)
2024-09-01 07:36:53
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;
}
最新文章
- 转载:ActiveMQ的可靠性机制
- 第三章 对象(JavaScript:语言精粹)
- Emmet语法大全手册
- IIS7显示ASP的详细错误信息到浏览器
- 经典算法系列--kmp
- Core Bluetooth Programming Guide
- 【转】Cocos2d-x 2.x CCSprite 灰白图的生成(利用shader设置)&mdash;&mdash;2013-08-27 21
- Stanford CoreNLP--Split Sentence
- Linux shell (一)
- 分享到QQ空间、新浪微博、腾讯微博的代码!(收藏)
- Render和template?
- 2.PHP 教程_PHP 安装
- 使用jQuery实现tag便签去重效果
- Flex 弹性盒模型
- android动画特效之解决解决移动后闪烁现象,解决输入法弹出后位置回复原状,解决两个动画叠加
- VUE2.0实现购物车和地址选配功能学习第五节
- LeetCode4. Median of Two Sorted Arrays---vector实现O(log(m+n)--- findkth
- IScroll.js 学习笔记
- mysql 5.7~默认sql_mode解读
- GD32 ------ 使用外部中断,中断函数需要延时才能读到真正电平
热门文章
- 软考之信息安全工程师(包含2016-2018历年真题详解+官方指定教程+VIP视频教程)
- 英特尔&#174; 实感™ 摄像头 (F200) 应用如何实现最佳用户体验
- 常用JDBC数据库驱动包和类名
- mysql中latin1编码中文转utf8
- Scrum立会报告+燃尽图(Beta阶段第二周第五次)
- scrum立会报告+燃尽图(第三周第三次)
- 20162328蔡文琛 week06 大二
- Android开发第二阶段(1)
- Ubuntu环境下No module named &#39;_tkinter&#39;错误的解决
- centos快速安装lamp