Code:
#include<bits/stdc++.h>
#define maxn 1000003
using namespace std;
char str[maxn],strtot[maxn];
int lentot,scc,flag,pp=0;
int st[maxn],ed[maxn],C[30][30],vis[maxn],vised[maxn],pre[maxn],low[maxn];
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
vector<int>G[maxn],answer,tag[maxn];
stack<int>S;
struct Trie
{
int cnt;
int ch[maxn][30];
void ins(char p[],int o)
{
int len=strlen(p+1),cur=0;
for(int i=1;i<=len;++i)
{
int c=p[i]-'a';
if(!ch[cur][c])
{
ch[cur][c]=++cnt;
G[cur].push_back(c);
}
cur=ch[cur][c];
}
tag[cur].push_back(o);
}
}trie;
void tarjan(int u)
{
S.push(u);
vised[u]=1;
pre[u]=low[u]=++scc;
for(int i=0;i<27;++i)
{
if(u==i || !vis[i] || !C[u][i]) continue;
if(!vised[i]) tarjan(i), low[u]=min(low[u], low[i]);
else if(vised[i]==1) low[u]=min(low[u],pre[i]);
}
if(low[u]==pre[u])
{
int cc=0;
for(;;)
{
int x=S.top();S.pop();
++cc;
vised[x]=-1;
if(cc>1) flag=1;
if(x==u) break;
}
}
}
bool check()
{
flag=scc=0;
while(!S.empty())S.pop();
for(int i=0;i<27;++i) vised[i]=low[i]=pre[i]=0;
for(int i=0;i<27;++i)
{
if(!vis[i]) continue;
if(!vised[i]) tarjan(i);
}
return flag^1;
}
void dfs(int u,int depth)
{
if(tag[u].size())
{
if(check())
{
for(int j=0,sz=tag[u].size();j<sz;++j) answer.push_back(tag[u][j]);
}
return;
}
for(int i=0;i<27;++i)
{
// 有延申出去的单词
if(trie.ch[u][i]) // 当前为 u
{
++vis[i];
int y=u;
for(int j=0,sz=G[u].size();j<sz;++j)
{
if(G[u][j]!=i)
{
++C[i][G[u][j]];
}
}
dfs(trie.ch[u][i],depth+1);
for(int j=0,sz=G[u].size();j<sz;++j)
{
if(G[u][j]!=i) --C[i][G[y][j]];
}
--vis[i];
}
}
}
int main()
{
// setIO("ok");
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",str+1);
trie.ins(str,i);
int v=strlen(str+1);
st[i]=lentot+1;
for(int j=1;j<=v;++j) strtot[++lentot]=str[j];
ed[i]=lentot;
}
dfs(0,0);
printf("%d\n",answer.size());
sort(answer.begin(),answer.end());
for(int i=0,sz=answer.size();i<sz;++i)
{
for(int j=st[answer[i]];j<=ed[answer[i]];++j) printf("%c",strtot[j]);
printf("\n");
}
return 0;
}