Restoration of string (composition + thinking)

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
to the
lru
cf
Output
cfmailru
Input
3
cake
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.

The meaning of the question: Constructing a string str requires that the given string is the string of the string. Among all the strings of str, the frequency of occurrence is the highest. If there are multiple answers, output

Output lexicographical order, the smallest one.

Idea: All letters in the formed string can only appear once. First, each string is formed into a directional chain, indicating that the previous letter can reach the next letter

, and then traverse all the string chains, connecting the words formed by these chains is the desired string

Code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
const int N =  1e5+10;
string a[N],ans;
int in[30],out[30];
int force 30, p[30];
int d[30][30];
int n;
int Dfs(int now){
	vis[now]=1;
	ans=ans+(char)(now+'a');
	for(int i=0;i<26;i++){
		if(d[now][i]){
			if(vis[i]) return 0; //It has been visited, indicating that other chains have this letter, and the letter appears many times
			return Dfs(i);
		}
	}
	return 1;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin>>a[i];
		int len=a[i].length();
		for(int j=0;j<len-1;j++){
			int x=a[i][j]-'a';
			int y=a[i][j+1]-'a';
			d[x][y]=1;//Build a directed edge from x->y
		}
		if(len==1) p[a[i][0]-'a']=1; //single point
	}
	
	for(int i=0;i<26;i++)
	   if(d[i][i]){ //Judge the self-loop, indicating that the changed letter has appeared many times
	   	 printf("NO\n");
	   	 return 0;
	   }
	
	for(int i=0;i<26;i++)
	for(int j=0;j<26;j++)
	   if(d[i][j]) in[j]++,out[i]++;
	
	for(int i=0;i<26;i++)
	   if(in[i]>1||out[i]>1){
	   	  printf("NO\n");
	   	  return 0;
	   }
	
	ans="";
	for(int i=0;i<26;i++){ //Add letters from small to large to ensure that the lexicographical order of the new string is the smallest
		if(out[i]!=0&&in[i]==0){ //Start from a string chain, starting from the starting point, the in-degree is 0, and the out-degree is 1
			if(!Dfs(i)){
				printf("NO\n");
				return 0;
			}
		}
		if(p[i]&&in[i]==0&&out[i]==0) ans=ans+(char)(i+'a'); //only one letter
	}
	
	for(int i=0;i<26;i++){
		if(out[i]!=0||in[i]!=0){
			if(!vis[i]){ //Not marked in DFS, indicating that a ring is formed
				printf("NO\n");
				return 0;
			}
		}
	}
	cout<<ans<<endl;   
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324610526&siteId=291194637