欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[SDOI2007]游戏【哈希+DAG拓扑】

程序员文章站 2022-03-14 19:52:38
...

题目链接


先通过哈希确定点,这里我使用的是双值哈希。然后利用哈希判断可以和前面的出现的点如何链接。

之后构造出来的图一定是一副DAG图,有向无环图,所以直接拓扑排序DP即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const ull h1 = 29, h2 = 31;
const int maxN = 1e4 + 7;
int N = 0, num[26];
ull b1[27], b2[27];
char s[maxN][105];
map<pair<ull, ull>, int> mp;
pair<ull, ull> id[maxN];
void Pre_did()
{
    b1[0] = b2[0] = 1;
    for(int i=1; i<26; i++)
    {
        b1[i] = b1[i - 1] * h1;
        b2[i] = b2[i - 1] * h2;
    }
}
int head[maxN], cnt = 0, du[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN << 1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
int dp[maxN], pre[maxN], ans_val, ans_id;
queue<int> Q;
inline void tp()
{
    while(!Q.empty()) Q.pop();
    for(int i=1; i<=N; i++) if(!du[i]) { Q.push(i); dp[i] = 1; pre[i] = 0; ans_val = 1; ans_id = i; }
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(dp[u] + 1 > dp[v])
            {
                dp[v] = dp[u] + 1;
                pre[v] = u;
            }
            if(dp[v] > ans_val)
            {
                ans_val = dp[v];
                ans_id = v;
            }
            du[v]--;
            if(!du[v]) Q.push(v);
        }
    }
}
int main()
{
    Pre_did();
    while(scanf("%s", s[++N]) != EOF)
    {
        du[N] = 0; head[N] = -1;
        int len = (int)strlen(s[N]);
        memset(num, 0, sizeof(num));
        for(int i=0; i<len; i++) num[s[N][i] - 'a']++;
        ull hash_val_1 = 0, hash_val_2 = 0, tmp_1, tmp_2;
        for(int i=0; i<26; i++)
        {
            hash_val_1 = hash_val_1 + (ull)num[i] * b1[i];
            hash_val_2 = hash_val_2 + (ull)num[i] * b2[i];
        }
        id[N] = MP(hash_val_1, hash_val_2);
        mp[id[N]] = N;
        for(int i=0; i<26; i++)
        {
            tmp_1 = hash_val_1 - b1[i]; tmp_2 = hash_val_2 - b2[i];
            if(mp[MP(tmp_1, tmp_2)])
            {
                du[mp[MP(tmp_1, tmp_2)]]++;
                addEddge(N, mp[MP(tmp_1, tmp_2)]);
            }
            tmp_1 = hash_val_1 + b1[i]; tmp_2 = hash_val_2 + b2[i];
            if(mp[MP(tmp_1, tmp_2)])
            {
                du[N]++;
                addEddge(mp[MP(tmp_1, tmp_2)], N);
            }
        }
        if(!mp[MP(hash_val_1, hash_val_2)]) mp[MP(hash_val_1, hash_val_2)] = N;
    }
    N--;
    tp();
    printf("%d\n", ans_val);
    while(ans_id)
    {
        printf("%s\n", s[ans_id]);
        ans_id = pre[ans_id];
    }
    return 0;
}