• 【LG3234】[HNOI2014]抄卡组


    题面

    题解

    分三种情况:

    若所有串都没有通配符,直接哈希比较即可。

    若所有串都有通配符,
    把无通配符的前缀 和 无通配符的后缀哈希后比较即可。
    中间部分由于通配符的存在,一定可以使所有串匹配。

    若部分串有通配符,
    首先把所有无通配符的字符串比较好。
    现在问题变为,能否通过通配符使每个串变为一个模板串。
    首先把前后缀比较好,然后就是中间部分。
    其实只需要让有通配符的串的中间部分与模板串匹配就可以合法。
    匹配暴力(O(n))即可。

    (参考(YCB)的题解)

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring> 
    #include <cmath> 
    #include <algorithm> 
    #include <vector>
    #include <string>
    using namespace std; 
    
    const unsigned long long X = 60923;
    unsigned long long val[10000010];
    const int MAX_N = 1e5 + 5;
    string s[MAX_N], s1[MAX_N];
    vector<unsigned long long> h[MAX_N];
    vector<int> pos[MAX_N];
    inline int cmp(const string &lhs, const string &rhs) { return lhs.length() < rhs.length(); }
    int T, n;
    
    void calc(int i) {
        pos[i].clear(), h[i].clear();
        pos[i].push_back(-1), h[i].push_back(0);
        for (string::iterator it = s[i].begin(); it != s[i].end(); ++it) {
            h[i].push_back(h[i].back() * X + *it);
            if (*it == '*')
                pos[i].push_back(it - s[i].begin());
        }
        pos[i].push_back(s[i].length());
    }
    
    inline unsigned long long Hash(const vector<unsigned long long> &vec, int l, int r) {
        ++l, ++r;
        return vec[r] - vec[l - 1] * val[r - l + 1];
    }
    
    bool check(int x, int y) {
        int lenx = s[x].length(), leny = s[y].length();
        if (s[y].find('*') != string::npos)
            swap(x, y), swap(lenx, leny);
        if (s[x].find('*') == string::npos && s[y].find('*') == string::npos)
            return Hash(h[x], 0, s[x].length() - 1) == Hash(h[y], 0, s[y].length() - 1);
        else {
            string A = "";
            string::size_type p = 0;
            for (int i = 1; i < pos[x].size(); i++) {
                int tpos = p, len = pos[x][i] - pos[x][i - 1] - 1;
                while (tpos + len - 1 < s[y].length() &&
                       Hash(h[x], pos[x][i - 1] + 1, pos[x][i] - 1) != Hash(h[y], tpos, tpos + len - 1))
                    ++tpos;
                if (tpos + len - 1 >= s[y].length()) return false;
                if (tpos != 0 && p == 0) return false;
                p = tpos + len;
            }
            return true;
        }
    }
    
    void Doit() {
        int pos = -1;
        for (int i = 1; i <= n; i++) calc(i);
        for (int i = 1; i <= n; i++)
            if (s[i].find('*') == string::npos) { pos = i; break; }
        if (pos == -1) {
            for (int i = 1; i <= n; i++) {
                s1[i] = "";
                for (int j = 0; j < s[i].length(); j++)
                    if (s[i][j] == '*') break;
                    else s1[i] += s[i][j];
            }
            sort(s1 + 1, s1 + n + 1, cmp);
            for (int i = 2; i <= n; i++) {
                for (int j = 0; j < s1[i - 1].length(); j++)
                    if (s1[i][j] != s1[i - 1][j])
                        return (void)(cout << 'N' << endl);
            }
    
            for (int i = 1; i <= n; i++) {
                s1[i] = "";
                for (int j = s[i].length() - 1; ~j; j--)
                    if (s[i][j] == '*') break;
                    else s1[i] += s[i][j];
            }
            sort(s1 + 1, s1 + n + 1, cmp);
            for (int i = 2; i <= n; i++) {
                for (int j = 0; j < s1[i - 1].length(); j++)
                    if (s1[i][j] != s1[i - 1][j])
                        return (void)(cout << 'N' << endl);
            }
        } else
            for (int i = 1; i <= n; i++) {
                if (i == pos) continue;
                if (!check(i, pos)) return (void)(cout << 'N' << endl);
            }
        cout << 'Y' << endl;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin >> T, val[0] = 1;
        for (int i = 1; i <= 10000000; i++) val[i] = val[i - 1] * X;
        while (T--) {
            cin >> n;
            for (int i = 1; i <= n; i++) cin >> s[i];
            Doit();
        }
        return 0;
    }
    
  • 相关阅读:
    学习Mybatis中的一对多表关联
    学习Mybatis中的一对一表关联
    学习Mybatis中的动态sql
    学习Mybatis中的约定大于配置、数据库配置优化、定义别名、类型处理器、resultMap和parameterType
    第八周进度
    构建之法阅读笔记07
    正则表达式
    梦断代码之阅读笔记02
    顶会热词统计
    本周进度
  • 原文地址:https://www.cnblogs.com/heyujun/p/10431502.html
Copyright © 2020-2023  润新知