【习题 5-10 UVA-1597】Searching the Web

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串出现的行数,以及用来判断这个字符串在这一段中存不存在。

->这里有一个地方要注意,千万不要在未确定这个字符串是否存在之前,调用mmap[i][s],因为这样,不管s存不存在,s都会加那么一个键值。
->而这就使得我们不能用更快的mmap[i].find(s)函数来寻找某个字符串在不在了.
->用mmap[i][s]访问,然后判断在不在的方式是会TLE的。

样例有9个'-'但实际上输出10个'-';
然后就是各个数据段之间的分隔符的输出。
or和and的输出都要用set来重新排序。
即从小的行开始到大的行依序输出。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 100;

int n, m;
vector <string> v[N + 10];
set <int> vv;
map <string, vector<int> > mmap[N + 10];
string s, ts;

bool have(int idx, string s)
{
    if (mmap[idx].find(s)==mmap[idx].end())
        return false;
    else
        return true;
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n; cin.get();
    for (int ii = 1; ii <= n; ii++)
    {
        int tot = 0;
        while (getline(cin, s))
        {
            if (s == "**********") break;
            v[ii].push_back(s);

            int len = s.size();
            for (int i = 0; i < len; i++)
                if (isalpha(s[i]))
                    s[i] = tolower(s[i]);
                else
                    s[i] = ' ';
            stringstream temp(s);
            string x;
            while (temp >> x)
            {
                mmap[ii][x].push_back(tot);
            }
            tot++;
        }
    }

    cin >> m; cin.get();
    for (int i = 0; i < m; i++)
    {
        getline(cin, ts);
        int fi = ts.find(' ', 0);
        if (fi == -1)//find x
        {
            bool ok = false;
            for (int j = 1; j <= n; j++)
                if (have(j, ts))
                {
                    if (ok) cout << "----------" << endl;
                    ok = true;
                    vv.clear();
                    for (int x : mmap[j][ts]) vv.insert(x);
                    for (int x : vv) cout << v[j][x] << endl;
                }
            if (!ok) cout << "Sorry, I found nothing." << endl;
            cout << "==========" << endl;
        }
        else
        {
            int fi2 = ts.find(' ', fi + 1);
            if (fi2 == -1)//not x
            {
                bool ok = false;
                ts = ts.substr(fi);
                while (ts[0] == ' ') ts.erase(0, 1);
                for (int j = 1; j <= n; j++)
                    if (!have(j, ts))
                    {
                        if (ok) cout << "----------" << endl;
                        ok = true;
                        int lenv = v[j].size();
                        for (int k = 0; k < lenv; k++)
                            cout << v[j][k] << endl;
                    }
                if (!ok) cout << "Sorry, I found nothing." << endl;
                cout << "==========" << endl;
            }
            else // x y z
            {
                stringstream ss(ts);
                string x, y, z;
                ss >> x; ss >> y; ss >> z;
                if (y == "OR")
                {
                    bool ok = false;
                    for (int j = 1; j <= n; j++)
                        if (have(j, x) || have(j, z))
                        {
                            if (ok) cout << "----------" << endl;
                            ok = true;
                            vv.clear();
                            if (have(j,x))for (int t : mmap[j][x]) vv.insert(t);
                            if (have(j,z))for (int t : mmap[j][z]) vv.insert(t);
                            for (int t : vv) cout << v[j][t] << endl;
                        }
                    if (!ok) cout << "Sorry, I found nothing." << endl;
                    cout << "==========" << endl;
                }
                else
                {
                    bool ok = false;
                    for (int j = 1; j <= n; j++)
                        if (have(j, x) && have(j, z))
                        {
                            if (ok) cout << "----------" << endl;
                            ok = true;
                            vv.clear();
                            for (int t : mmap[j][x]) vv.insert(t);
                            for (int t : mmap[j][z]) vv.insert(t);
                            for (int t : vv) cout << v[j][t] << endl;
                        }
                    if (!ok) cout << "Sorry, I found nothing." << endl;
                    cout << "==========" << endl;
                }
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7682658.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值