codeforces856 B Similar Words 模型转换 tree图建图+树形dp

B. Similar Words
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Let us call a non-empty sequence of lowercase English letters a word. Prefix of a word x is a word y that can be obtained from x by removing zero or more last letters of x.

Let us call two words similar, if one of them can be obtained from the other by removing its first letter.

You are given a set S of words. Find the maximal possible size of set of non-empty words X such that they satisfy the following:

each word of X is prefix of some word from S;
X has no similar words.
Input
Input data contains multiple test cases. The first line of the input data contains an integer t — the number of test cases. The descriptions of test cases follow.

The first line of each description contains an integer n — the number of words in the set S (1 ≤ n ≤ 106). Each of the following n lines contains one non-empty word — elements of S. All words in S are different.

It is guaranteed that the total length of all words in one input data doesn’t exceed 106.

Output
For each test case print one line that contains one integer m — the maximal number of words that X can contain.

Example
input
2
3
aba
baba
aaab
2
aa
a
output
6
1

题意:
题意有点迷。
给出n个串,要求你从中选出一些前缀 使得这些前缀都不‘相似’ A与B相似 当且仅当 A(B) 去掉首字母后==B(A)

解题思路
题目给出的输入和数据范围很像AC自动机之类的题目。 想往这方面思考。
考虑建出tire图后,任意一个节点都是一个前缀,两个点A,B不能同时存在的条件是 fail[A] = B 且deep[A]-1=deep[B]
然后再努(bai)力(du)一下
将不能同时取的点之间都连一条边
模型就转换成了 任意两个相邻点不能同时取到。
然后再努力思考一下
可以得出 按照上述建图方法,建出来的不可能是图,只有可能是树或森林。
那就很好办了,经典树形dp模板题。
感觉很有意思。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 2e6 + 5;
const int maxn = 2e6 + 100;
const int SIGMA_SIZE = 26;
const int MAXNODE = 1000005;
const int MAXS = 1e6+10;
struct ACautomata {

    int ch[MAXNODE][SIGMA_SIZE];
    int f[MAXNODE];    // fail函数
    //int val[MAXNODE];  // 每个字符串的结尾结点都有一个非0的val
    int last[MAXNODE]; // 输出链表的下一个结点
    int deep[MAXNODE];
    int sz;
    //int d[MAXNODE];
    void init() {
        sz = 1;
        memset(ch[0], 0, sizeof (ch[0]) );
        //memset(d, 0, sizeof(d));
    }

    // 字符c的编号
    inline int idx (char c) {
        return c - 'a';
    }

    // 插入字符串。v必须非0
    void insert (char *s) {
        int u = 0, n = strlen (s),dlen=0;
        for (int i = 0; i < n; i++) {
            int c = idx (s[i]);
            dlen++;
             //printf("%c", s[i]);
            if (!ch[u][c]) {
                memset (ch[sz], 0, sizeof (ch[sz]) );
                deep[sz]=dlen;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
            //printf("%d", u);
            //puts("");
        }
    }
    // 计算fail函数
    void getFail() {
        queue<int> q;
        f[0] = 0;
        // 初始化队列
        for (int c = 0; c < SIGMA_SIZE; c++) {
            int u = ch[0][c];
            if (u) {
                f[u] = 0;
                q.push (u);
                last[u] = 0;
            }
        }
        // 按BFS顺序计算fail
        while (!q.empty() ) {
            int r = q.front();
            q.pop();
            for (int c = 0; c < SIGMA_SIZE; c++) {
                int u = ch[r][c];
                if (!u) {
                    ch[r][c] = ch[f[r]][c];
                    continue;
                }
                q.push (u);
                int v = f[r];
                while (v && !ch[v][c]) v = f[v];
                f[u] = ch[v][c];
                //last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
        }
    }
    long long dp[2][MAXS];
    bool vis[MAXS];
    class Edge{
        public:
        int v,nxt;
    };
    Edge edge[MAXS<<2];
    int tot;
    int head[MAXS];
    void add(int u,int v){
        edge[tot].nxt=head[u];
        edge[tot].v=v;
        head[u]=tot;
        tot++;
    }
    void build_tree(){
        for(int i=0;i<=sz;i++){
            vis[i]=0;
            head[i]=-1;
        }
        tot=0;
        for(int i=1;i<sz;i++){
            int fa = f[i];
            //cout<<fa<<","<<i<<endl;
            if((deep[fa]==deep[i]-1) && deep[fa]!=0){
                add(fa,i);
                add(i,fa);
                //cout<<i<<"->"<<fa<<endl;
            }
        }
    }
    void dfs(int u,int pre){
        vis[u]=1;
        dp[1][u]=1;
        dp[0][u]=0;
        long long sum =0 ;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v= edge[i].v;
            if(v==pre) continue;
            dfs(v,u);
            dp[1][u]+=dp[0][v];
            dp[0][u]+=max(dp[0][v],dp[1][v]);
        }
    }
    void solve(){
        long long ans =0;
        for(int i=1;i<sz;i++){
            if(!vis[i]){
                dfs(i,-1);
                ans+=max(dp[0][i],dp[1][i]);
            }
        }
        printf("%lld\n",ans);
    }
} ac;

char s[MAXN];



int main() {
    int n;
    int T;
    cin>>T;
    while(T--) {
        cin >> n;
        ac.init();
        for (int i = 1; i <= n; i++) {
            scanf ("%s", s);
            ac.insert (s);
        }
        ac.getFail();
        ac.build_tree();
        ac.solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值