Moves in squared strings (I) -- 7Kyu
来源:互联网 发布:js.10000网上营业厅 编辑:程序博客网 时间:2024/05/17 05:09
原题
http://www.codewars.com/kata/56dbe0e313c2f63be4000b25/train/cpp
题目
This kata is the first of a sequence of four about “Squared Strings”.
You are given a string of n lines, each substring being n characters long: For example:
s = “abcd\nefgh\nijkl\nmnop”
We will study some transformations of this square of strings.
Vertical mirror: vert_mirror (or vertMirror or vert-mirror)
vert_mirror(s) => “dcba\nhgfe\nlkji\nponm”
Horizontal mirror: hor_mirror (or horMirror or hor-mirror)
hor_mirror(s) => “mnop\nijkl\nefgh\nabcd”
high-order function oper(fct, s) where
fct is the function of one variable f to apply to the string s (fct will be one of vertMirror, horMirror)
s = “abcd\nefgh\nijkl\nmnop”
oper(vert_mirror, s) => “dcba\nhgfe\nlkji\nponm”
oper(hor_mirror, s) => “mnop\nijkl\nefgh\nabcd”
分析
水平镜像是以\n为分割符,将字符串逆序重排即可,考虑到最后一个没有\n,可以拼接上一个\n,直接循环重排,最后将串的最后一个字符pop
垂直镜像只需将字符串整体逆序重排,然后水平镜像即可。
代码
class Opstrings{typedef std::function<std::string(const std::string)> func;public: static std::string vertMirror(const std::string &strng); static std::string horMirror(const std::string &strng); static std::string oper(func f, const std::string &s);}; std::string Opstrings::horMirror(const std::string &strng){ std::string str(strng); reverse(begin(str),end(str)); str=vertMirror(str); return str; } std::string Opstrings::vertMirror(const std::string &strng){ std::string str=strng+"\n"; auto it_begin = begin(str); auto it_end = begin(str); while(it_end!= end(str)){ if(*it_end == '\n'){ std::reverse(it_begin,it_end); std::cout<<*(it_end+1)<<" "<<*it_begin<<std::endl; it_begin = it_end+1; } it_end++; } str.pop_back(); return str; } std::string Opstrings::oper(func f, const std::string &s){ std::string str(f(s)); return str; }
他人思路
- 字符串矩阵镜像非常简单,主要分为三步,分割->交换位置->合并
- oper()主要是把函数作为参数
代码
#include <iostream>#include <string>#include <sstream>#include <vector>#include <iterator>#include <algorithm>using namespace std;string horMirror(const string &str){ // 字符串以回车换行分割成数组 istringstream iss(str); vector<string> str_vec; str_vec.assign(istream_iterator<string>(iss),istream_iterator<string>()); // 交换字符串位置 swap_ranges(begin(str_vec),(str_vec.begin()+str_vec.size()/2),str_vec.rbegin()); // 把字符串数组组成字符串 ostringstream oss; copy(begin(str_vec),end(str_vec),ostream_iterator<string>(oss,"\n")); string res = oss.str(); res.pop_back(); // 除去最后一个回车。 return res;}string vertMirror(const string &str){ // 字符串以回车换行分割成数组 istringstream iss(str); vector<string> str_vec; str_vec.assign(istream_iterator<string>(iss),istream_iterator<string>()); // 遍历字符串 for(auto& s:str_vec){ // 交换字符位置 swap_ranges(begin(s),(s.begin()+s.size()/2),s.rbegin()); } // 把字符串数组组成字符串 ostringstream oss; copy(begin(str_vec),end(str_vec),ostream_iterator<string>(oss,"\n")); string res = oss.str(); res.pop_back(); // 除去最后一个回车。 return res;}string oper(string (func)(const string &),const string &s){ return func(s);}作者:jdzhangxin链接:http://www.jianshu.com/p/64cfab1dada2來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。说明1.字符串分割成向量的C++惯用法(流默认以空格、回车、Tab作为分割符)。2.容器范围数据交换swap_range()。3.容器元素连接成字符串。
- Moves in squared strings (I) -- 7Kyu
- Consecutive strings -- 6 kyu
- How do I compare strings in Java?(Stackoverflow)
- Is this a triangle? -- 7kyu
- Parts of a list -- 7 kyu
- 2017青岛区域赛I题The Squared Mosquito Coil
- Sum of numbers from 0 to N -- 7 kyu
- Compare strings in java
- Multiline strings in JavaScript
- Declaring strings in GoAsm
- Strings in Swift 2
- 2017ACMICPC亚洲区青岛站:I:The Squared Mosquito Coil【模拟题】
- UVa 10679 - I Love Strings!!
- Flexible Pattern Matching in Strings
- Creating Attributed Strings in Cocoa
- Marshal UTF8 Strings in .NET
- Strings in C and C++
- Using attributed strings in iOS6
- Java多线程--资源限制可能影响多线程效率
- leetcode 42. Trapping Rain Water 正反循环遍历求解
- TCP和UDP的区别
- [贪心]codeforces
- 关于abstract为什么不能和static连用
- Moves in squared strings (I) -- 7Kyu
- 拦截导弹(规律:下降子序列的个数等于最长上升子序列的长度)
- 使用 eclipse 编写 xtend
- SSM整合时的xml配置参考
- 计算机视觉资源文件--链接整理
- V4,V7,V13兼容包
- Nginx负载均衡
- Android 二级图片缓存,图片优化,图片异步加载框架设计
- Vue2 自定义全局指令Vue.directive和指令的生命周期