华为机试(仿LISP字符串运算)
2017-06-28 22:55
417 查看
[编程|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)
时间限制: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; }
相关文章推荐
- 2017华为机试(仿LISP字符串运算)
- 2017华为机试(仿LISP字符串运算)
- (华为机试)整数字符串加减运算
- 华为机试 - 计算字符串表达的运算
- 字符串四则运算表达式的计算(华为机试)
- 字符串四则运算表达式的计算(华为机试)
- 华为机试——通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 字符串四则运算表达式的计算(华为机试)
- 华为机试-字符串的过滤
- 华为机试-字符串中最大最小数之和
- 华为机试——超大整数加法运算
- 华为机试—给定字符串中找出单词
- 华为机试模拟题--在一个字符串中删除去包含另一个字符串的所有的字符
- 华为招聘机试整理10:实现字符串中子字符串的替换
- 华为机试在线训练-牛客网(14)删除字符串中出现次数最少的字符
- 【华为机试】字符串最后一个单词的长度
- 华为机试—字符串中找出单词排序
- 华为机试——字符串拆分
- 华为机试-字符串中大数之和
- 2014华为机试-判断输入的字符串是不是一个有效的IP地址