华为机试(仿LISP字符串运算)

[编程|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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值