#include <bits/stdc++.h>
using namespace std;
vector< vector<int> >adj(200) ;
int vis[200] , st[200] , indegree[200] , linked[200][200];
bool prefix(string s , string s1)
{
if(s.size() > s1.size())
return false ;
for(int i = 0 ; i < s.size() ; ++i)
{
if(s[i] != s1[i])
return false ;
}
return true ;
}
bool cycle(int node)
{
vis[node] = 1 ;
st[node] = 1 ;
for(auto &child : adj[node])
{
if(!vis[child])
{
if(cycle(child))
return true ;
}
else if(st[child])
return true ;
}
st[node] = 0 ;
return false ;
}
vector<int>topo ;
void topological_sort(int node)
{
vis[node] = 1 ;
for(auto &child : adj[node])
{
if(vis[child])
continue ;
topological_sort(child) ;
}
topo.push_back(node) ;
}
int main()
{
int n ;
cin>>n ;
string words[n] , arr[n];
for(int i = 0 ; i < n ; ++i)
cin>>words[i] ;
for(int i = 0 ; i < n ; ++i)
{
int idx ;
cin>>idx ;
idx-- ;
arr[i] = words[idx] ;
}
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < i ; ++j)
{
if(prefix(arr[i] , arr[j]))
return cout<<"NE" , 0 ;
}
}
//construct graph
vector<int>v ;
for(int i = 0 ; i < n-1 ; ++i)
{
if(prefix(arr[i] , arr[i+1]))
continue ;
int j = i+1 ;
for(int k = 0 ; ; ++k)
{
if(arr[i][k] != arr[j][k])
{
int c = arr[i][k] - 'a' , c2 = arr[j][k] - 'a' ;
if(linked[c2][c] == 1)
break ;
linked[c2][c] = 1 ;
adj[c2].push_back(c) ;
indegree[c]++ ;
break;
}
}
}
for(int i = 0 ; i < 26 ; ++i)
{
memset(vis , 0 , sizeof(vis));
if(cycle(i))
{
return cout<<"NE\n" , 0 ;
}
}
memset(vis , 0 , sizeof(vis)) ;
for(int i = 0 ; i < 26 ; ++i)
{
if(indegree[i] == 0 && vis[i] == 0)
topological_sort(i) ;
}
string ans = "" ;
for(int i = 0 ; i < 26 ; ++i)
ans += 'a' ;
//debugging
if(topo.size() < 26)
while(1) ;
//debugging
if(topo.size() > 26)
while(1) ;
for(int i = 0 ; i < 26 ; ++i)
{
//debugging
if(topo[i] >= 26 || topo[i] < 0)
while(1) ;
ans[topo[i]] = ((char)(i+'a')) ;
}
cout<<"DA\n";
cout<<ans ;
return 0 ;
}
Compilation message
cezar.cpp: In function 'bool prefix(std::__cxx11::string, std::__cxx11::string)':
cezar.cpp:12:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < s.size() ; ++i)
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |