Cattle off more school fifth H subsequence 2 topological sorting

Meaning of the questions:

Give you the length of the string up to 1000, this string has up to 10 letters, each time you two kinds of letters, output the relative position of these two letters in the string, the string you ask what was originally way, if there is no output -1

answer:

The whole string as set, each character is considered as a collection element, seen in the context string partial order, making this Hasse set, topologically sorted Hasse. If it is found when the topology is not a point to point to 0 degree, and it outputs 1;

#include<bits/stdc++.h>
using namespace std;
char ch[20];
int n,m,len[202],st[202];
char ss[200][20002],ans[20002];
int pos[20][202],cnt[20];
bool check(int u)
{
    for(int i=1;i<m;i++)
    {
        int id=pos[u][i];
        if(st[id]>len[id]||ss[id][st[id]]!=u+'a')return false;
    }
    return true;
}
void work(int u)
{
    for(int i=1;i<m;i++)
    {
        int id=pos[u][i];
        st[id]++;
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m*(m-1)/2;i++)
    {
        scanf("%s",ch);
        int c=ch[0]-'a',d=ch[1]-'a';
        pos[c][++cnt[c]]=i;
        pos[d][++cnt[d]]=i;
        cin>>len[i],st[i]=1;
        if(len[i])scanf("%s",ss[i]+1);
    }
    for(int i=1;i<=n;i++)
    {
        int t=-1;
        for(int j=0;j<m;j++)if(check(j)){t=j;break;}
        if(t==-1){puts("-1");return 0;}
        ans[i]=t+'a';
        work(t);
    }
    for(int i=1;i<=m*(m-1)/2;i++)if(st[i]<=len[i]){puts("-1");return 0;}
    cout<<ans+1<<endl;
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11290897.html