题目1029:魔咒词典

不涉及算法,正确的输入和存储数据,然后进行查找即可。

#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>

using namespace std;

struct Item
{
    string word;
    string func;
};

int main()
{
    string s;
    int count;
    vector<Item> str;
    while (cin >> s)
    {
        if (s == "@END@") break;
        string k;
        getline(cin,k);
        Item temp;
        temp.word = s;
        string real_k;
        for (int i = 1;i < k.size();i++)
        {
            real_k += k[i];
        }
        temp.func = real_k;
        str.push_back(temp);
    }
    int n;
    cin >> n;
    vector<string> ss;
    for (int i = 0;i < n;i++)
    {
        string m;
        getline(cin,m);
        ss.push_back(m);
    }
    for (int i = 1;i <= n;i++)
    {
        string out;
        if (ss[i][0] == '[')
        {
            int j;
            for (j = 0;j < str.size();j++)
            {
                if (ss[i] == str[j].word)
                {
                    cout << str[j].func << endl;
                    break;
                }
            }
            if (j == str.size()) cout << "what?" << endl;
        }
        else
        {
            int k;
            for (k = 0;k < str.size();k++)
            {
                if (ss[i] == str[k].func)
                {
                    for (int p = 1;p < str[k].word.size()-1;p++)
                    {
                        cout << str[k].word[p];
                    }
                    cout << endl;
                    break;
                }
            }
            if (k == str.size()) cout << "what?" << endl;
        }
    }
}

本题如果利用c++中的String和STL类库中的map处理的话会极为方便

#include<iostream>
#include<string>
#include<algorithm>
#include<map>

using namespace std;

int main()
{
    string n,m,sub;
    int num;
    map<string,string> s;
    while (getline(cin,n) && n != "@END@")
    {
        int a,b;
        a = n.find('[');
        b = n.find(']');
        sub = n.substr(a+1,b-1-a);
        m = n.substr(b+2,n.size()-b-2);
        s[sub] = m;
        s[m] = sub;
    }
    cin >> num;
    getline(cin,n);              //这里需要读走缓冲区中的回车键
    for (int i = 0;i < num;i++)
    {
        getline(cin,n);
        if (n[0] == '[')
        {
            n.erase(0,1);
            n.erase(n.size()-1,1);
        }
        if (s.find(n) == s.end()) cout << "what?" <<endl;
        else cout << s[n] << endl;
    }
}
参考:

http://blog.csdn.net/u013615904/article/details/25637593

http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值