原题

https://www.codewars.com/kata/550f22f4d758534c1100025a/train/cpp

题目

Once upon a time, on a way through the old wild west,…

… a man was given directions to go from one point to another. The directions were “NORTH”, “SOUTH”, “WEST”, “EAST”. Clearly “NORTH” and “SOUTH” are opposite, “WEST” and “EAST” too. Going to one direction and coming back the opposite direction is a needless effort. Since this is the wild west, with dreadfull weather and not much water, it’s important to save yourself some energy, otherwise you might die of thirst!

How I crossed the desert the smart way.

The directions given to the man are, for example, the following:

[“NORTH”, “SOUTH”, “SOUTH”, “EAST”, “WEST”, “NORTH”, “WEST”].

[“WEST”]

Other examples:

In [“NORTH”, “SOUTH”, “EAST”, “WEST”], the direction “NORTH” + “SOUTH” is going north and coming back right away. What a waste of time! Better to do nothing.

The path becomes [“EAST”, “WEST”], now “EAST” and “WEST” annihilate each other, therefore, the final result is [] (nil in Clojure).

In [“NORTH”, “EAST”, “WEST”, “SOUTH”, “WEST”, “WEST”], “NORTH” and “SOUTH” are not directly opposite but they become directly opposite after the reduction of “EAST” and “WEST” so the whole path is reducible to [“WEST”, “WEST”].

Task

Write a function dirReduc which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).

The Haskell version takes a list of directions with data Direction = North | East | West | South. The Clojure version returns nil when the path is reduced to nothing. The Rust version takes a slice of enum Direction {NORTH, SOUTH, EAST, WEST}.
Example:

dirReduc([“NORTH”, “SOUTH”, “SOUTH”, “EAST”, “WEST”, “NORTH”, “WEST”]) => [“WEST”]
dirReduc([“NORTH”, “SOUTH”, “SOUTH”, “EAST”, “WEST”, “NORTH”]) => []

See more examples in “Example Tests”

Note
Not all paths can be made simpler. The path [“NORTH”, “WEST”, “SOUTH”, “EAST”] is not reducible. “NORTH” and “WEST”, “WEST” and “SOUTH”, “SOUTH” and “EAST” are not directly opposite of each other and can’t become such. Hence the result path is itself : [“NORTH”, “WEST”, “SOUTH”, “EAST”].

分析

该题可以使用括弧匹配的思想去做,利用栈来储存方向,栈顶元素和path字符串方向相反则弹出,最后将栈内元素放到vector中,再将vector反向后,即为最终方向

代码

class DirReduction
{
public:static std::vector<std::string> dirReduc(std::vector<std::string> &arr);
};
std::vector<std::string> DirReduction::dirReduc(std::vector<std::string> &arr){std::stack<std::string> s;/*std::function<const std::string(const std::string&)>*/auto opposite=[](const std::string& str){if(str.compare("WEST")==0) return std::string("EAST");if(str.compare("EAST")==0) return std::string("WEST");if(str.compare("NORTH")==0) return std::string("SOUTH");if(str.compare("SOUTH")==0) return std::string("NORTH");return std::string(" ");};s.push(std::string());for(auto v:arr){if(s.top()==opposite(v))s.pop();elses.push(v);}arr.clear();while(!s.empty()){arr.push_back(s.top());s.pop();}arr.pop_back();//删除空stringstd::reverse(begin(arr),end(arr));return arr;
}

Directions Reduction -- 5 kyu相关推荐

  1. Dimensionality Reduction - Principle Component Analysis problem formulation

    摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第十五章<降维>中第117课时<主成分分析问题规划>的视频原文字幕.为本人在视频学习过程中记录下来 ...

  2. [论文翻译]A Global Geometric Framework for Nonlinear Dimensionality Reduction

    论文题目:A Global Geometric Framework for Nonlinear Dimensionality Reduction 论文来源:Science 290, 2319 (200 ...

  3. TVM Reduction降低算力

    TVM Reduction降低算力 这是有关如何降低算力TVM的介绍材料.像sum / max / min这样的关联约简运算符是线性代数运算的典型构造块. 本文将演示如何降低TVM算力. from f ...

  4. 使用LDA(Linear Discriminant Analysis)进行降维(dimention reduction)详解和实战

    使用LDA(Linear Discriminant Analysis)进行降维(dimention reduction)详解和实战 LDA也可被视为一种监督降维技术.不同于PCA(Principle ...

  5. HDLBits 系列(6)(Reduction)缩位运算符

    目录 抛砖引玉 Reduction在奇偶校验中的应用 抛砖引玉 您已经熟悉两个值之间的按位运算,例如a&b或a ^ b. 有时,如果向量很长,您想创建一个对一个向量的所有位进行操作的宽门,例如 ...

  6. [Share]Web Directions Conferences

    the audio files and presentation slides are now available from the Web Directions website- Developme ...

  7. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  8. 数字图像处理实验(11):PROJECT 05-02,Noise Reduction Using a Median Filter

    实验要求: Objective: To understand the non-linearity of median filtering and its noise suppressing abili ...

  9. TensorFlow学习笔记(十九) 基本算术运算和Reduction归约计算

    基本运算,变量由tf.constant函数转化为1阶张量.然后计算:现在用tf.reduce_prod()和tf.reduce_sum()函数重新定义,当给定某个tensor张量作为输入时,这些函数会 ...

  10. 文献记录(part17)--VARCLUST: clustering variables using dimensionality reduction

    学习笔记,仅供参考,有错必究 关键词:子空间聚类,降维,主成分分析,贝叶斯信息准则,k-centroids VARCLUST: clustering variables using dimension ...

最新文章

  1. asp.net 服务器应用程序不可用
  2. ftp 的三种数据传输模式
  3. 多行显示的UIButton
  4. AsyncTask 实现异步处理任务
  5. 奇怪的问题,再次启动jar包会导致bean对象失效?Unknown redis exception Cannot connect, Event executor group is terminated
  6. React 16 加载性能优化指南
  7. 微信开发 Java SDK
  8. C# CRC16 在textbox中显示实时时间
  9. 修改ftp服务器用户,通过批处理修改FTP账号和密码
  10. Python中的字符串特性(索引、切片、重复、连接、成员操作符号)
  11. js实现select动态添加option,默认为选中状态
  12. JAVA实现邮箱注册功能
  13. 测试经验总结分享(长期更新)
  14. 渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
  15. 未来6英寸主战场,碳化硅-精密划片机
  16. ubuntu 触摸板失灵解决
  17. 虚拟机安装---模板机准备1(最小化安装)
  18. 统计学习方法-感知机概括和补充
  19. 计算机 打开3389端口,3389端口修改和打开【图文教程】
  20. Java开发工具下载地址

热门文章

  1. jde多目标_CVPR 2020 多目标跟踪算法JDE 训练
  2. 美国计算机硕士要读多久,去美国读研究生需要多久 各专业时长一览
  3. 华为5.0以上设备(亲测有效)激活xposed框架的流程
  4. 【matlab实现多种股票数据同列收盘价格分析走势图,以及涨跌幅变化曲线第二篇】
  5. html设置首缩选进两字符,首行缩进2字符怎么设置 css
  6. JVM学习笔记 之 JVM概述
  7. zencart 模板文件说明
  8. [音乐欣赏]夜空中最亮的星,张杰,苏阳,新闻晚高峰结束曲
  9. Matlab中pickic_高颜值甜品DIY |春季甜品Picnic野餐系列,一起过个惬意慵懒的午后时光❗️...
  10. IPFS独角兽西部世界解读:销毁的近2000万FIL去哪里了?