C - The Dominator of Strings

Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string SS is dominated by TT if SS is a substring of TT.

Input

The input contains several test cases and the first line provides the total number of cases. 
For each test case, the first line contains an integer NN indicating the size of the set. 
Each of the following NN lines describes a string of the set in lowercase. 
The total length of strings in each case has the limit of 100000100000. 
The limit is 30MB for the input file.

Output

For each test case, output a dominator if exist, or No if not.

Sample Input

3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac

Sample Output

youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No

这是一道AC自动机模板题,但由于我们队太菜,没人会AC自动机,只能用KMP暴力,结果在关了同步后竟然让我们给卡过来,不过这样也反映出我们一个很大的问题,很多算法只会用板子,有些算法甚至只知道名字—_—

接下来的是我们卡过去的代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<string>
using namespace std;

#define endl '\n'

long long t,n;
struct node{
	string x;
	int len;
}a[100001];
int nxt[100001];
void get_nxt(string t,int* nxt)
{
	int p=t.size();
	nxt[0]=-1;
	int k=-1,j=0;
	while(j<p)
	{
		if(-1==k||t[k]==t[j])
		{
			k++;
			j++;
			if(t[j]!=t[k])
				nxt[j]=k;
			else
				nxt[j]=nxt[k];
		}
		else
		    k=nxt[k];
	}
}
int kmp(string s,string t)
{
	int i,j;
	int len=t.size(),lens=s.size();
	
	get_nxt(t,nxt);
	i=j=0;
	while(i<lens&&j<len)
	{
		if(j==-1||s[i]==t[j])
		{
			i++;
			j++;
		}
		else
			j=nxt[j];
	}
	if(j>=len)
		return 1;
	return 0;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n;
		long long max1=0,k;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i].x;
			a[i].len=a[i].x.size();
			if(max1<a[i].len)
			{
				max1=a[i].len;
				k=i;
			}
		}
		int flag=1;
		for(int i=1;i<=n;i++)
		{
			if(i==k)
			continue;
			if(!kmp(a[k].x,a[i].x))
			{
				flag=0;
				break;
			}
			
		}
		if(flag==1)
		cout<<a[k].x << endl;
		else
		cout<<"No"<< endl;
	}

}

 

之后的是AC自动机的代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Trie
{
    int nxt[26];
    int cnt, fail;
    void init()
    {
        for (int i = 0; i < 26; ++i)
            nxt[i] = -1;
        cnt = fail = 0;
    }
} L[N];
char s[N];
int st[N], Len[N];
int tot;
 
namespace AC
{
    void init()
    {
        tot = 0;
        L[tot++].init();
    }
    void insert(char s[], int len)
    {
        int u = 0;
        for (int i = 0; i < len; ++i)
        {
            int v = s[i] - 'a';
            if (L[u].nxt[v] == -1)
            {
                L[tot].init();
                L[u].nxt[v] = tot++;
            }
            u = L[u].nxt[v];
        }
        ++L[u].cnt;
    }
    void build()
    {
        L[0].fail = 0;
        queue<int>Q;
        for (int i = 0; i < 26; ++i)
        {
            if (L[0].nxt[i] == -1)
                L[0].nxt[i] = 0;
            else
            {
                L[L[0].nxt[i]].fail = 0;
                Q.push(L[0].nxt[i]);
            }
        }
        while (!Q.empty())
        {
            int u = Q.front();
            Q.pop();
            int uf = L[u].fail;
            for (int i = 0; i < 26; ++i)
            {
                int v = L[u].nxt[i];
                if (v == -1)
                    L[u].nxt[i] = L[uf].nxt[i];
                else
                {
                    L[v].fail = L[uf].nxt[i];
                    Q.push(v);
                }
            }
        }
    }
    int query(char s[], int len)
    {
        int ret = 0;
        int u = 0;
        for (int i = 0; i < len; ++i)
        {
            int v = s[i] - 'a';
            u = L[u].nxt[v];
            while (u && L[u].nxt[v] == -1)
                u = L[u].fail;
            int t = u;
            while (t && L[t].cnt != -1)
            {
                ret += L[t].cnt;
                L[t].cnt = -1;
                t = L[t].fail;
            }
        }
        return ret;
    }
}
int main(void)
{
    int T, n, i;
    scanf("%d", &T);
    while (T--)
    {
        AC::init();
        scanf("%d", &n);
        int sum = 0;
        int Maxlen = 0;
        int ID = 0;
        for (i = 0; i < n; ++i)
        {
            scanf("%s", s + sum);
            Len[i] = strlen(s + sum);
            AC::insert(s + sum, Len[i]);
            st[i] = sum;
            sum += Len[i];
            if (Len[i] > Maxlen)
            {
                ID = i;
                Maxlen = Len[i];
            }
        }
        AC::build();
        if (AC::query(s + st[ID], Len[ID]) == n)
        {
            int ed = st[ID] + Len[ID];
            for (i = st[ID]; i < ed; ++i)
                printf("%c", s[i]);
            puts("");
        }
        else
            puts("No");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值