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.
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.
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.
4 mail ai lru cf
cfmailru
3 kek preceq cheburek
NO
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;
}
}