题目1029:魔咒词典

来源:互联网 发布:按键精灵文字输出源码 编辑:程序博客网 时间:2024/06/11 01:29

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

#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
原创粉丝点击