HDU6208 The Dominator of Strings【字符串】

题意:问是否有一个字符串包含其他字符串


思路:有的话,那一定是最长的那一个。最长的有多个的话,那这几个一定要相同;否则,不存在。跑一下AC自动机,把最长的看成目标串,别的看成模式串,看目标串中出现几次模式串。


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair pii;
using namespace std;

struct Trie
{
    int next[100010][26],fail[100010],end[100010];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 26;i++)
            next[L][i] = -1;
        end[L++] = 0;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(string &buf)
    {
        int len = buf.size();
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][buf[i]-'a'] == -1)
                next[now][buf[i]-'a'] = newnode();
            now = next[now][buf[i]-'a'];
        }
        end[now]++;
    }
    void build()
    {
        queueQ;
        fail[root] = root;
        for(int i = 0;i < 26;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while( !Q.empty() )
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 26;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(string &buf)
    {
        int len = buf.size();
        int now = root;
        int res = 0;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]-'a'];
            int temp = now;
            while( temp != root )
            {
                res += end[temp];
                end[temp] = 0;
                temp = fail[temp];
            }
        }
        return res;
    }
};
char buf[100010];
Trie ac;
vector v;
vector ind;
int da;
int main()
{
    int T,n;
    string s;
    scanf("%d",&T);
    while( T-- )
    {
        scanf("%d",&n);
        ind.clear();
        v.clear();
        da = -1;
        getchar();
        for(int i = 0; i < n; i++)
        {
            gets(buf);
            int len = strlen(buf);
            if(len > da)
            {
                da = len;
                ind.clear();
                ind.push_back(i);
            }
            else if(len == da)
                ind.push_back(i);
            v.push_back(buf);
        }
        int flag = 1;
        if(v.size() > 1)
        {
            for(int i = 0; i < ind.size(); i++)
            {
                if(i > 0 && v[ind[i]] != v[ind[i-1]])
                {
                    flag = 0;
                    break;
                }
            }
        }
        if(!flag)
            printf("No\n");
        else
        {
            int ans = ind[0];
            ac.init();
            for(int i = 0;i < n;i++)
            {
                if(i != ans)
                    ac.insert(v[i]);
            }
            ac.build();
            int num = ac.query(v[ans]);
            if(num == n-1)
                cout<


你可能感兴趣的:(网络赛,字符串)