HDU 1251 统计难题

题意:输入字符串(单词表, 每行不超过10字符), 读到空行结束; 输入询问(字符串),处理到文件末尾, 对每次询问输出单词表中以该字符串为前缀的单词个数。

首先要解决的是读到空行结束的问题:每行都是以 ' ' 为结尾, 统计字符 ‘ ’的个数, 若连续出现两次则退出

其次是前缀, 不超过10字符, 则统计单词表中每行字符串的所有前缀出现次数,用map实现

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> p;
typedef long double ld;
#define mem(x) memset(x, 0, sizeof(x))
#define me(x) memset(x, -1, sizeof(x))
#define fo(i,n) for(i=0; i<n; i++)
#define sc(x) scanf("%lf", &x)
#define pr(x) printf("%lld
", x)
#define pri(x) printf("%lld ", x)
#define lowbit(x) x&-x
const ll MOD = 1e18 +7;
const ll N = 6e6 +5;
map<string,ll> mp;
int main()
{
    ll i, j, k, l=0;
    ll n, m, t, x;
    string s, s1;
    char c;
    ll co=0;
    while(1)
    {
        scanf("%c", &c);
        if(c=='
') co++;
        else s+=c, co=0;
        if(co==2) break;
        if(co==1)
        {
            n=s.size();
            for(i=1; i<=n; i++)
                s1=s.substr(0,i), mp[s1]++;
            s.clear();
        }
    }
    while(cin>>s)
    {
        cout<<mp[s]<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/op-z/p/10756146.html