[CodeVS3013]单词背诵 做题笔记

4 篇文章 0 订阅
1 篇文章 0 订阅

题目来源:http://codevs.cn/problem/3013/

题目描述 Description 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词。

文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的只算一个)。并且在背诵的单词量尽量多的情况下,还要使选出的文章段落尽量短,这样她就可以用尽量短的时间学习尽可能多的单词了。

输入描述 Input Description 第1行一个数n,

接下来n行每行是一个长度不超过10的字符串,表示一个要背的单词。

接着是一个数m,

然后是m行长度不超过10的字符串,每个表示文章中的一个单词。

输出描述 Output Description
输出文件共2行。第1行为文章中最多包含的要背的单词数,第2行表示在文章中包含最多要背单词的最短的连续段的长度。

样例输入 Sample Input 3

hot

dog

milk

5

hot

dog

dog

milk

hot

样例输出 Sample Output 3

3

数据范围及提示 Data Size & Hint 对于30%的数据 n<=50,m<=500;

对于60%的数据 n<=300,m<=5000;

对于100%的数据 n<=1000,m<=100000;

字符串hash+某种奇怪的区间统计O(n)
hash函数可以把每个小写字母字符串表示成一个独一无二的27进制数。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
const int MOD=5000000;
typedef unsigned long long LL;
int n,m,cnt=0,t=0,ans=0,ANS=0x7fffffff,v[MOD+2];
char ch[12];
struct Hash {
    int num,col;LL val;
    bool mark;
}hash[MOD+2];
LL godigit (char *s) {
    LL ans=0;
    for (int i=0;i<strlen(s);i++) {
        ans=ans*27+(int)s[i]-97;
    }
    return ans;
}
int HASH (LL val) {
    int x=val%MOD;
    while (hash[x].num&&hash[x].val!=val) x=(x+1)%MOD;
    return x;
}

int main () {
    LL tmp=0;int x;
    scanf("%d",&n);
    for (int i=1;i<=n;i++) {
        scanf("%s",ch);
        for (int i=0;i<strlen(ch);i++) ch[i]=tolower(ch[i]);
        tmp=godigit(ch);
        x=HASH(tmp);
        hash[x].num++;
        hash[x].val=tmp;
    }
    scanf("%d",&m);
    for (int i=1;i<=m;i++) {
        scanf("%s",ch);
        for (int j=0;j<strlen(ch);j++) ch[j]=tolower(ch[j]);
        tmp=godigit(ch);
        x=HASH(tmp);
        v[i]=x;//可以直接根据文章中单词的编号来找hashtable中的值
        if (hash[x].num&&!hash[x].mark) { ++cnt;hash[x].mark=1; }
    }
    printf("%d\n",cnt);
    int l=0,r=1;
    while (r<=m) {
        while (r<=m) {
            if (hash[v[r]].mark&&!hash[v[r]].col) ++ans;
            hash[v[r]].col++;
            r++;
            if (ans==cnt) break;
        }
        while ((!hash[v[l]].mark||hash[v[l]].col>1)&&l<r) { hash[v[l]].col--;l++; }
        ANS=min(ANS,r-l);
    }

    printf("%d\n",ANS);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值