POJ3087(KB1-G 简单搜索)
Shuffle'm Up
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10366 | Accepted: 4800 |
Description
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.
The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:
The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.
After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.
For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.
Output
Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.
Sample Input
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1
Source
//2017-02-27
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set> using namespace std; string shuffle(string &s1, string &s2, int len)
{
string tmp(*len, ' ');
for(int i = ; i < *len; i++)
{
if(i%==)tmp[i] = s1[i/];
else tmp[i] = s2[i/];
}
for(int i = ; i < len; i++)
s1[i] = tmp[i];
for(int i = ; i < len; i++)
s2[i] = tmp[i+len];
return tmp;
} int main()
{
int T, n, ans;
string s1, s2, s12, tmp;
cin >> T;
for(int kase = ; kase <= T; kase++)
{
cin>>n>>s1>>s2>>s12;
set<string> s;
ans = ;
while(){
tmp = shuffle(s1, s2, n);
if(tmp == s12){
ans++;
break;
}
set<string>::iterator it = s.find(tmp);
if(it != s.end()){
ans = -;
break;
}else{
s.insert(tmp);
ans++;
}
}
cout<<kase<<" "<<ans<<endl;
} return ;
}
POJ3087(KB1-G 简单搜索)的更多相关文章
- ElasticSearch 5学习(4)——简单搜索笔记
空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...
- 和我一起打造个简单搜索之SpringDataElasticSearch入门
网上大多通过 java 操作 es 使用的都是 TransportClient,而介绍使用 SpringDataElasticSearch 的文章相对比较少,笔者也是摸索了许久,接下来本文介绍 Spr ...
- 和我一起打造个简单搜索之ElasticSearch集群搭建
我们所常见的电商搜索如京东,搜索页面都会提供各种各样的筛选条件,比如品牌.尺寸.适用季节.价格区间等,同时提供排序,比如价格排序,信誉排序,销量排序等,方便了用户去找到自己心里理想的商品. 站内搜索对 ...
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- 分布式搜索ElasticSearch构建集群与简单搜索实例应用
分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...
- solr简单搜索案例
solr简单搜索案例 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字搜索商品信息,根据商品分类.价格过滤搜索结果,也可以根据价格进行排序,实现分页. 架构分为: 1. solr服务器 2. ...
- 和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮
前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文 ...
- 和我一起打造个简单搜索之Logstash实时同步建立索引
用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 J ...
- 和我一起打造个简单搜索之IK分词以及拼音分词
elasticsearch 官方默认的分词插件,对中文分词效果不理想,它是把中文词语分成了一个一个的汉字.所以我们引入 es 插件 es-ik.同时为了提升用户体验,引入 es-pinyin 插件.本 ...
- 和我一起打造个简单搜索之ElasticSearch入门
本文简单介绍了使用 Rest 接口,对 es 进行操作,更深入的学习,可以参考文末部分. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch ,这个版本比较稳 ...
随机推荐
- poj 107 DNA sorting
关于Java的题解,也许效率低下,但是能解决不只是ACGT的序列字符串 代码如下: import java.util.*; public class Main { public static void ...
- Jmeter监控系统等资源,ServerAgent端口的本次启动端口修改
默认情况下在下载的ServerAgent下,如果服务是windows系统,则直接启动"startAgent.bat"即可,如果是Linux系统,则直接启动"./start ...
- fullpage.js与animate.css搭配使用
jquery的fullpage.js插件的使用 https://alvarotrigo.com/fullPage/#3rdPage 官网 https://github.com/alvarotrig ...
- 【hdu6058】 Kanade's sum 模拟
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题目大意:给你一个$1$到$n$的排列,请求出该序列所有区间中第$k$大之和,若该区间内少于$ ...
- 12、xamarin form中实现H5 网页唤醒微信支付的方法
在微信的支付中有种支付叫微信H5支付.方便用户在网页中轻松唤起微信进行支付. 当然微信不推荐大家使用这样的方式唤起微信支付.建议app还是使用正常的微信支付sdk即可 服务端与其他的建议参考微信支付官 ...
- JavaScript中的垃圾回收机制与内存泄露
什么是内存泄露? 任何编程语言,在运行时都需要使用到内存,比如在一个函数中, var arr = [1, 2, 3, 4, 5]; 这么一个数组,就需要内存. 但是,在使用了这些内存之后, 如果后面他 ...
- Java之集合(十)EnumMap
转载请注明源出处:http://www.cnblogs.com/lighten/p/7371744.html 1.前言 本章介绍Map体系中的EnumMap,该类是专门针对枚举类设计的一个集合类.集合 ...
- 【Tomcat】使用记录
同时绑定ipv4和ipv6 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&q ...
- python中如何打印某月日历
Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历: import calendar cal = calendar.month(2017, 10) print ("以下 ...
- 2018春招-美团后台开发方向编程题 (python实现)
第一题:字符串距离 题目: 给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量.如串”aab”与串”aba”的距离为 2:串”ba”与串”aa”的距离为 1: ...