华为机试(仿LISP字符串运算)
来源:互联网 发布:sql内连接的意义 编辑:程序博客网 时间:2024/06/15 04:30
[编程|300分] 仿LISP字符串运算
时间限制:3秒
空间限制:32768K
题目描述
LISP语言唯一的语法就是括号要配对。
形如 (OP P1 P2 …),括号内元素由单个空格分割。
其中第一个元素OP为操作符,后续元素均为其参数,参数个数取决于操作符类型
注意:参数 P1, P2 也有可能是另外一个嵌套的 (OP P1 P2 …)
当前OP类型为 quote / reverse / search / combine 字符串相关的操作:
- quote: 引用一个字符串,即返回字符串本身内容
参数个数 1
- reverse: 把字符串反转,并返回
参数个数 1
- search: 在第一个字符串中查找第二个字符串的第一次出现,返回从这开始到结束的所有字符串
如果查找不到,返回空字符串
参数个数 2
- combine: 把所有字符串组合起来
参数个数不定,但至少 1 个
其中P1, P2 等参数可能是带双引号的字符串,如 “abc”,也有可能是另外一个 (OP P1 P2 …)
上述字符串包括引号;引号中间的所有字符,均为 ASCII 可打印字符,且不会再出现引号 (“)
输出也为带双引号的字符串
举例:
输入字符串 输出结果
(quote “!@#
(reverse “a b c”) “c b a”
(search “abcdef” “cd” ) “cdef”
(search “abcdef” “xy” ) “”
(combine “a” “b” “cde) “) “abcde) ”
(search (combine “1234567890” “abcdefgh” “1234567890”) (reverse “dc”)) cdefgh123456789
输入描述:
合法C字符串,字符串长度不超过512;用例保证了无语法错误.
输出描述:
合法C字符串,需要带括号
输入例子:
(search “huawei” “we”)
输出例子:
“wei”
代码:(做的时候,只通过部分,以下是事后修改的代码,不知是否能AC)
#include <algorithm>#include <iostream>#include <string>#include <stack>#include <vector>int main(){ char chs[512]; gets(chs); //在牛客OJ上,用std::getling(std::cin, str);会出错 std::string str(chs); std::stack<std::string> op; std::stack<std::string> strs; for (int i = 0; i < str.size();) { if (str[i] == ' ') { i = i + 1; continue; } if (str[i] == '(') { strs.push(std::string("(")); int spaceIndex = str.find(' ', i); std::string tmp = str.substr(i + 1, spaceIndex - i - 1); op.push(tmp); i = spaceIndex; } else if (str[i] == ')') { std::string curOp = op.top(); op.pop(); std::vector<std::string> tmps; //存放操作的字符串 while (strs.top() != "(") { tmps.push_back(strs.top()); strs.pop(); } strs.pop(); if (curOp == "quote") { strs.push(tmps.back()); } else if (curOp == "reverse") { std::reverse(tmps.back().begin(), tmps.back().end()); strs.push(tmps.back()); } else if (curOp == "search") { std::string str1 = tmps.back(); std::string str2 = tmps[tmps.size() - 2]; auto index = str1.find(str2); if (index == std::string::npos) { strs.push(""); } else { strs.push(str1.substr(index)); } } else if (curOp == "combine") { std::string tmp; for (int i = tmps.size() - 1; i >= 0; --i) { tmp += tmps[i]; } strs.push(tmp); } ++i; } else { auto index = str.find('"', i + 1); strs.push(str.substr(i + 1, index - i - 1)); i = index + 1; } } std::cout << "\"" << strs.top() << "\"" << std::endl; system("pause"); return 0;}
- 华为机试(仿LISP字符串运算)
- 2017华为机试(仿LISP字符串运算)
- (华为机试)整数字符串加减运算
- 华为机试 - 计算字符串表达的运算
- 华为机试之计算字符串运算结果
- (华为机试)字符串压缩
- (华为机试)字符串转换
- 华为机试---字符串过滤
- 华为机试:字符串系列
- 华为机试---字符串过滤
- 华为机试:查找字符串
- 华为机试--字符串加密
- 华为机试--字符串排序
- 华为机试---字符串通配符
- 华为机试---字符串匹配
- 华为机试---字符串计数
- 华为机试---字符串替换
- 华为机试 字符串分隔
- Genymotion与android studio
- Android源码分析
- ACM学习心得
- 卢卡斯定理(Lucas)
- 流程控制(部分)
- 华为机试(仿LISP字符串运算)
- docopt详解
- RxJS中的forkJoin
- Maven的生命周期 命令行与生命周期
- leetcode598. Range Addition II
- spring MVC-1
- 大数阶乘(C语言实现)
- Spring、Spring MVC、MyBatis整合文件配置详解
- 被虐到死去活来的Mysql乱码问题