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

Greater New York 2006

暴力搜索即可

 1 //2017-02-27
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <set>
 6
 7 using namespace std;
 8
 9 string shuffle(string &s1, string &s2, int len)
10 {
11     string tmp(2*len, ‘ ‘);
12     for(int i = 0; i < 2*len; i++)
13     {
14         if(i%2==1)tmp[i] = s1[i/2];
15         else tmp[i] = s2[i/2];
16     }
17     for(int i = 0; i < len; i++)
18           s1[i] = tmp[i];
19     for(int i = 0; i < len; i++)
20           s2[i] = tmp[i+len];
21     return tmp;
22 }
23
24 int main()
25 {
26     int T, n, ans;
27     string s1, s2, s12, tmp;
28     cin >> T;
29     for(int kase = 1; kase <= T; kase++)
30     {
31         cin>>n>>s1>>s2>>s12;
32         set<string> s;
33         ans = 0;
34         while(1){
35             tmp = shuffle(s1, s2, n);
36             if(tmp == s12){
37                 ans++;
38                 break;
39             }
40             set<string>::iterator it = s.find(tmp);
41             if(it != s.end()){
42                 ans = -1;
43                 break;
44             }else{
45                 s.insert(tmp);
46                 ans++;
47             }
48         }
49         cout<<kase<<" "<<ans<<endl;
50     }
51
52     return 0;
53 }
时间: 2024-08-10 10:15:06

POJ3087(KB1-G 简单搜索)的相关文章

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

和我一起打造个简单搜索之ElasticSearch集群搭建

我们所常见的电商搜索如京东,搜索页面都会提供各种各样的筛选条件,比如品牌.尺寸.适用季节.价格区间等,同时提供排序,比如价格排序,信誉排序,销量排序等,方便了用户去找到自己心里理想的商品. 站内搜索对于一个网站几乎是标配,只是搜索的强大与否的区别,有的网站只支持关键词模糊搜索,而淘宝,京东提供了精细的筛选条件,同时支持拼音搜索等更方便的搜索方式. 由于笔者在一家做网络文学的公司工作,所以实现就是以小说为商品的搜索,具体可以参考起点网小说的搜索. 如图所示,起点网的搜索提供了关键词搜索和排序条件以

和我一起打造个简单搜索之SpringDataElasticSearch入门

网上大多通过 java 操作 es 使用的都是 TransportClient,而介绍使用 SpringDataElasticSearch 的文章相对比较少,笔者也是摸索了许久,接下来本文介绍 SpringDataElasticSearch 的 api 使用,更加方便的进行查询. 系列文章 一.和我一起打造个简单搜索之ElasticSearch集群搭建 二.和我一起打造个简单搜索之ElasticSearch入门 三.和我一起打造个简单搜索之IK分词以及拼音分词 四.和我一起打造个简单搜索之Log

专题一、简单搜索 - Virtual Judge

很久以前刷完了Virtual Judge上的简单搜索专题,现总结如下: POJ 1321 由于题目的数据范围比较小,可以直接dfs暴力.读入时记录每个空位的位置,保存在pX[]以及pY[]数组中.暴力的时候统计当前处理第几个空格以及当前处理到了第几行即可. #include <iostream> #include <memory.h> using namespace std; const int MAX = 128; long long ans; int N, K, nCnt; b

java 模拟简单搜索

Java 模拟简单搜索 实体类 package org.dennisit.entity; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:[email protected]">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 下午04:51:03 * * @function: TODO * */ public class Medicine { private I

lucene4.3简单搜索示例代码

原文:lucene4.3简单搜索示例代码 源代码下载地址:http://www.zuidaima.com/share/1550463715560448.htm   示例代码,请牛哥们多

参与过三次搜索引擎发展转折的百度,为什么要回归“简单搜索”

相信在搜索引擎技术产品化的二十余年间,我们早已经习惯了搜索引擎的商业化.不过在最近的数博会上,李彦宏却展示了一款名为"简单搜索"的语音交互搜索App,并且声明简单搜索永无广告. 相信很多人也曾想象过这样一款产品--通过语音发问,得出最简单的答案.不过可能大多数人没有想到,推出这款产品的是百度.很长一段时间内,百度的搜索引擎商业化一直为人诟病.但这次百度率先推出永无广告的语音交互搜索,无疑是在搜索引擎的技术和产品形式上都进行了重要创新. 当我们在质疑大企业的创新能力时常常会忽视一点,在技

poj 3279 Fliptile (简单搜索)

Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in whic

和我一起打造个简单搜索之Logstash实时同步建立索引

用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 Java API 的方式建立索引,也可以通过 Logstash 的插件 logstash-input-jdbc 完成,今天来探讨下如何使用 logstash-input-jdbc 完成全量同步以及增量同步. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch

和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮

前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文章 一.和我一起打造个简单搜索之ElasticSearch集群搭建 二.和我一起打造个简单搜索之ElasticSearch入门 三.和我一起打造个简单搜索之IK分词以及拼音分词 四.和我一起打造个简单搜索之Logstash实时同步建立索引 五.和我一起打造个简单搜索之SpringDataElasti