【Kickstart】2018 Round A -Scrambled Words

解法

首先,通过(start,end,counter)来标识每个单词。

坑点有三:

  1. 搜索时不需要遍历单词数组,只需要从S中截取字符串,判断它是否在单词的哈希集合里
  2. 生成字符串的a,b,c,d还有x得是长整型!!!
  3. 优化的点在于如何在长字符串里搜索,由于所有单词加在一起的长度不超过 1 0 5 10^5 105,所以单词长度的种类数不超过 1 0 2.5 10^{2.5} 102.5,当长度固定时,在S上做滑动窗口,比每次都单独substr要快。所以最后用一个set<int>存长度,用另一个unordered_map<WORD,int>来存每个单词的个数,每匹配上一个WORD就删除一个。
#include <stdio.h>
#include <unordered_map>
#include <string>
#include <iostream>
#include <memory.h>
#include <stdlib.h>
#include <set>
#include <functional>

using namespace std;

class WORD {
public:
    char s,e;
    unsigned short key[26];
    WORD(string str) {
        s = str[0]; e = str[str.size()-1];
        memset(key,0,sizeof(unsigned short)*26);
        for(auto &c:str) {
            key[c-'a']++;
        }
    }
    bool operator == (const WORD &b) const {
        if (s==b.s) {
            if (e==b.e) {
                for(int i=0;i<26;++i)
                    if (key[i]!=b.key[i]) return false;
                return true;
            }
            return false;
        }
        return false;
    }
};

namespace std {
    template <>
    class hash<WORD> {
    public:
        size_t operator () (const WORD &a) const {
            auto ch = hash<char>();
            auto un = hash<unsigned short>();
            size_t ans = ch(a.s);
            ans ^= ch(a.e);
            for(auto &c:a.key) {
                ans ^= un(c);
            }
            return ans;
        }
    };
}

int main() {
    int t;
    cin>>t;
    for(int round=1;round<=t;++round) {
        int l;
        cin>>l;
        unordered_map<WORD, int> dict;
        set<int> lengths;
        for(int i=0;i<l;++i) {
            string str;
            cin>>str;
            dict[WORD(str)]++;
            lengths.insert(str.size());
        }
        string s1,s2;
        int n;
        long long a,b,c,d;
        cin>>s1>>s2>>n>>a>>b>>c>>d;
        long long x1 = s1[0], x2 = s2[0];
        string str = s1+s2;
        for(int i=3;i<=n;++i) {
            int tmp = x2;
            x2 = (a*x2+b*x1+c)%d;
            x1 = tmp;
            str += char('a'+(x2%26));
        }
        int ans=0;
        for(auto &ll:lengths) {
            if (ll>n) break;
            WORD temp(str.substr(0,ll-1));
            int i=0;
            do {
                temp.e = str[i+ll-1];
                temp.key[temp.e-'a']++;
                auto it = dict.find(temp);
                if (it!=dict.end()) {
                    ans += it->second;
                    dict.erase(it);
                }
                temp.key[temp.s-'a']--;
                temp.s = str[++i];
            } while (i+ll-1<n);
        }
        printf("Case #%d: %d\n",round,ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值