D - Restoration of string 图论,拓扑序

D. Restoration of string
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring.

You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes).

A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string.

The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap.

String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set.

Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct.

The total length of the strings doesn't exceed 105.

Output

Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings.

Examples
Input
4
mail
ai
lru
cf
Output
cfmailru
Input
3
kek
preceq
cheburek
Output
NO
Note

One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.

仅仅只能分析出每一个串没有多于一个的字符,然后gg

看了下题解,发现大佬用拓扑序做的ORG

如果两个串没有一个多余的字符

如果他们两个有相同的元素并且不存在于前后缀中,并且第二个也不是第一个的子串,那么就说这两个不可能合并。(NO)

反之他们可以合并

那么反映在拓扑图上那么都是有序的,并且他们是一条一条的长链才说明他们是可以组成的,这样的话枚举开头拼接起来即可(YES)

如果有环,那么反映在拓扑图上就是有一个点的入/出度>1,当然这并不包括头结点。(NO)

如果头尾成环,那么这个环上的所有节点都是1,这也是不行的(NO)

那么26*26建一个邻接矩阵就行了,暴力跑一下即可

#include<bits/stdc++.h>
using namespace std;
string sv[123456],ans;
bool gra[30][30],sign[30],vis[30];
int in[30],out[30];
void dfs(int now)
{
    vis[now]=1;
    ans=ans+(char)('a'+now);
    for(int i=0; i<26; i++)
    {
        if(gra[now][i])
        {

            dfs(i);
        }
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        ans="";
        memset(sign,0,sizeof(sign));
        memset(gra,0,sizeof(gra));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++)
            cin>>sv[i];
        for(int i=1; i<=n; i++)
        {
            int len=sv[i].size();
            for(int j=0; j<len-1; j++)
            {
                gra[sv[i][j]-'a'][sv[i][j+1]-'a']=1;
            }
            if(len==1)
                sign[sv[i][0]-'a']=1;
        }
        for(int i=0; i<26; i++)
        {
            for(int j=0; j<26; j++)
            {
                if(gra[i][j])
                {
                    out[i]++;
                    in[j]++;
                }
            }
        }
        for(int i=0; i<26; i++)
        {
            if(in[i]>1||out[i]>1)
            {
                printf("NO\n");
                return 0;
            }
        }
        for(int i=0; i<26; i++)///目前只可能出现头尾环
        {
            if(in[i]==0&&out[i])///头
            {
                dfs(i);
            }
            if(sign[i]&&in[i]==0&&out[i]==0)
                ans=ans+(char)(i+'a');
        }
        for(int i=0; i<26; i++)
        {
            if(in[i]||out[i])
            {
                if(!vis[i])
                {
                    printf("NO\n");
                    return 0;
                }
            }
        }
        cout<<ans<<endl;

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值