ZOJ1004Anagrams by Stack

浙江大学ZOJ 1004题 详解 第一件事还是翻译题目,这道题目的意思大概是,有多组输入,每组输入有两行,第一行是一个单词(字符串)第二行是另外一个字符串(有可能组成字母都是相同的只不过是字母顺序不同,也有可能是另外的字符串,和原来的字符串没有多大关系),然后給你一个栈,你对这个栈和第一个字符串一共有两种操作, 第一种,压栈操作,执行这种操作会让第一个字符串的第一位放入栈中(就是说如果原来的字符串是madam,执行完一次压栈操作之后 阅读详情

Anagrams by Stack

Time Limit: 2000 msMemory Limit: 65536 KB

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:

[
i i i i o o o o
i o i i o o i o
]

where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.

Input

The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.

Output

For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.

Process

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item

We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o ois valid, but
i i o is not (it's too short), neither is
i i o o o i(there's an illegal pop of an empty stack)

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and owhich will produce the second member of each pair from the first.

Sample Input

madam
adamm
bahama
bahama
long
short
eric
rice

Sample Output

[
i i i i o o o i o o 
i i i i o o o o i o 
i i o i o i o i o o 
i i o i o i o o i o 
]
[
i o i i i o o i i o o o 
i o i i i o o o i o i o 
i o i o i o i i i o o o 
i o i o i o i o i o i o 
]
[
]
[
i i o i o i o o 
]
/*
本题需要注意理解题意,题中的意思是数列a中相邻两数的差必须囊括1到n-1。
*/
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;

int main()
{
    for(int n; cin >> n;) {
        int a[3000];
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            if (i > 0)
                a[i - 1] = abs(a[i] - a[i - 1]);
        }
        sort(a, a + n - 1);
        bool flag = true;
        for (int i = 0; i < n - 1; i++) {
            if (a[i] != i + 1)
                flag = false;
        }
        cout << (flag ? "Jolly" : "Not jolly") << endl;
    }
}

ZOJ1004-Anagrams by Stack(回溯) 如何根据一系列堆栈的操作实现回文构词法呢?有两种堆栈的操作,将单词TROT转换成TORT。 阅读详情

相关推荐

Drugbank XML文件解析

DrugBank Release Version 5.1.13 | DrugBank Online点击download下载XML文件,也可以使用COMMAND命令下载。(下载文件需要先注册一个账号文件,就可以下载了)下载好压缩文件,并解压会得到如下两个文件:full database.xml就是我们所需要的XML文件,完整的XML很大,大约为1.6G因为原始的文件非常大,直接查看XML文件是有困难的,所以我们使用一下命令来查看该文件的前一百行 得到如下输出 对于前三行 第一行:表示XML 版本为 1.0

qtjbzs的博客 594

ZOJ1004-Anagrams by Stack

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i i o o i o ] where i stands for Push

Dante__Alighieri的专栏 1765

《实战指南 | MES业务顾问蓝图设计方案:避免90%项目踩坑,从战略到落地的全攻略》

《MES业务顾问蓝图设计方案》干货分享:制造业高管常遇MES系统沦为"摆设"的困境,根源在于蓝图设计阶段未抓准业务痛点。本文系统梳理了6大关键点:1)战略定位上承ERP下接设备;2)通过现场蹲点挖掘真实需求;3)构建可扩展技术架构;4)建立数据闭环管理;5)组织保障分步实施;6)持续量化KPI优化。方案强调从业务价值出发,避免"纸上谈兵",通过数据驱动实现"计划-执行-反馈"闭环,最终让MES成为企业数字化的核心资产。

「东哥说MES」 专注于MES制造执行系统,为你拆解核心架构、功能模块及与ERP/PLC的集成逻辑。提供从入门到精通的系列干货、精选行业报告与实用工具,构建你的数字化知识体系,关注我,用实战经验照亮你的数字化之路。 1350

Anagrams by Stack(进栈出栈问题)

Anagrams by Stack How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:    [ i i i i o o o o i o i i o o i o ] w

小小黑 1276

Windows平台MuJoCo仿真环境配置与Qt集成实战

本文详细介绍了在Windows平台上配置MuJoCo物理仿真环境并与Qt框架集成的完整实战流程。内容涵盖从环境准备、库文件配置、项目创建到核心代码编写的每一步,重点解决了Windows下常见的编译链接错误和路径问题,帮助开发者在Windows系统中高效搭建机器人仿真与可视化控制开发环境。

weixin_29221753的博客 185

【网络与爬虫 62】小程序爬虫终极攻略:微信支付宝小程序数据采集完全指南

本文深入解析微信和支付宝小程序的爬虫技术,从基础架构分析到高级逆向工程,涵盖WXML/AXML模板解析、API接口拦截、网络流量分析等核心技术。通过实战案例和代码示例,帮助开发者掌握小程序数据采集的完整技术栈。

熵数实验室 3425

ZOJ1004 Anagrams by Stack

Anagrams by Stack 给出两个字符串a和b,需要将字符串a通过一系列入栈、出栈操作使得最终出栈的字符串与b相同,按字典序输出所有可实现方法的出入栈顺序。 思路: 求所有方案–>dfs搜索+回溯 只有当栈顶元素与目标串当前位置相同时,才进行出栈操作,故最终一定能够得到目标串! 注意输出:结尾要有空格! #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5

qq_51776588的博客 1002

保姆级AI辅助科研选题指南!利用Gemini 3 Pro选出可操作、新颖且有价值的高质选题(附AI提示词模板)

例如:【示例主题】。我是一名【身份,例如:博士生/研究者】,我的专业是【专业】,我正在撰写学术期刊文章,我关注的研究问题是:【研究问题】。我是一位xxx,专业是【专业】,我正在撰写学术期刊文章,我的研究问题是【研究问题】,请你充当学术写作专家,给我推荐一些能够解决我的研究问题的研究方法。我们的任务是借助Gemini 3 Pro围绕研究主题,提出具体的研究问题,可以按照研究问题的提出方法来对Gemini 3 Pro提问,也能直接利用开放性的思维直接对其提问,还可按照某种特定研究问题的示例提出具体的研究问题。

智能AI让学术科研更容易 585

zoj1004-Anagrams by Stack

zoj1004 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004

lvy的专栏 933

WPF vs Avalonia: 跨入C#桌面应用现代化的大门

请注意,这只是一个目录,详细内容将根据这个结构逐步展开。如果您需要某个特定部分的详细内容,请告知我,我将为您提供深入的解析和示例代码。WPF,全称Windows Presentation Foundation,是微软为Windows平台量身打造的UI框架。自2006年发布以来,它就像一位经验丰富的老船长,引领着桌面应用的航船在技术的海洋中乘风破浪。Avalonia,一个新兴的跨平台UI框架,它像一位年轻的探险家,勇敢地探索着桌面应用的新世界。

java专栏 2404

python全栈入门到实战【基础篇 01】Python初识:定位、优势与发展历程

本文介绍了热门编程语言Python。它是解释型高级语言,具通用、入门友好、胶水型特点,以简洁易读为设计哲学。优势包括语法简洁高效、生态庞大、跨平台、社区活跃、应用广泛、门槛低。发展历经诞生(1989-1994,吉多创建,1994年1.0发布)、成长(版本迭代,3.0发布)、崛起(Web与大数据推动)、爆发(AI浪潮助推)及持续迭代阶段。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 1万+

ZOJ 1004 Anagrams by Stack

给定一个输入单词,某些入栈和出栈操作序列是有效的,因为单词的每个字符都会被入栈和出栈,而且不会尝试弹出空堆栈。输入的结束以文件结尾为标志。对于每个输入对,你的程序应该生成一个排序的 i 和 o 有效序列列表,这些序列从源单词生成目标单词。给定一对单词,程序应该生成将第一个单词转换为第二个单词的堆栈操作序列。对于每一对输入单词,输出所有可能的操作序列,按照字典序排列,每个序列占一行,) 操作序列,使得给定的源单词可以通过这些操作序列转换成目标单词。序列,这些序列将从第一个单词对中产生每个单词对的第二个成员。

wuqingshun314159的博客 640

华为公司职位分析:财经数据管理专员

本文依据华为官方财经数据管理专员岗位职责、岗位要求撰写,区别于助理岗,本岗位侧重独立主导、体系搭建、方案全权设计、项目闭环落地、数据资产价值运营,贴合华为财经数字化、数据底座建设、财务数据治理核心体系,全面拆解岗位专业知识、核心技能、简历打法与高频面试应答。

王国平的博客 167

zoj1004 Anagrams by Stack

Anagrams by Stack Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Submit Status Description How can anagrams result from sequences of stack oper

acm_1361677193的专栏 475

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

zoj 1004 Anagrams by Stack

Anagrams by StackTime Limit: 2 Seconds      Memory Limit: 65536 KB How can anagrams result from sequences of stack operations? There are t...

代码虐我千万遍 我待代码如初恋 的博客 249

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

zoj_1004-Anagrams by stack

<div style="background-color: rgb(255, 255, 255); font-family: Arial, 'Apple SD Gothic Neo', 'Malgun Gothic', Helvetica, nanumgothic, sans-serif, Menlo, Monaco, monospace; font-size: 0.9em; overflow-x

feiqian在努力~ 749

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

ZOJ 1004 Anagrams by Stack

C - Anagrams by Stack Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1004 Description How can anagrams result from sequences of

CSU_ACM Solution Library 526

ZOJ1004_Anagrams by Stack

ZOJ1004 Anagrams by Stack 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 这题我的想法有点不太一样,如果直接用传说中的回溯,入栈出栈应该代码会很短,但是我用的思路比较不太一样。 我先从源字符串的最后开始 找 目标字符串的 第一位,然后利用找到字符在源字符串的当前位置和之前已有...

weixin_30555753的博客 133

ZOJ - 1004 Anagrams by Stack 【DFS】

Description How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i i o o i o ] whereista...

一定要站在自己热爱的生活里闪闪发光 3万+

ZOJ 1004 Anagrams by Stack 分析与解答

问题:How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:[i i i i o o o oi o i i o o i o]where i stand

the Pensieve 4464

zoj 1004 Anagrams by Stack (回溯)

开始觉得无从下手啊 毕竟自己是个菜鸟....用递归,回溯法程序写的有点乱,主要是变量命名时没有什么规则,让人看不懂,自己也懒得修改了, 注释也有点不清楚......原题:How can anagrams result from sequences of stack operations? There are two sequences of stack operators whic

Sooowhat 456
上一篇: ZOJ 1003 Crashing Balloon
下一篇: ZOJ1005Jugs
Lycodeboy
博客等级 码龄11年 1170粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值