AOJ 2534 Dictionary

21 篇文章 0 订阅

Aizu 2534

题意:给n个字符串,问是否存在一个合法的字典,使得这些字符串是字典序递增的。

对相邻两个字符串的第一个不相等字符建边,拓扑排序即可。
注意边界判断。

#include <bits/stdc++.h>
using namespace std;
const int MAXN=505;
char str[MAXN][15];
int in[26];
bool used[26][26];
int main(){
    int n, cnt;
    queue<int> q;
    while(cin>>n&&n){
        memset(in,0,sizeof in);
        memset(used,0,sizeof used);
        bool flag=true;
        for(int i=0; i<n; i++){
            scanf("%s",str[i]);
            int len=strlen(str[i]);
            if(i&&flag){
                for(int k=0; k<len; k++){
                    if(str[i][k]==str[i-1][k]){
                        continue;
                    }
                    if(!str[i-1][k]) goto ok;
                    int s1=str[i-1][k]-'a', s2=str[i][k]-'a';
                    if(used[s2][s1]) flag=false;
                    if(!used[s1][s2]){
                        used[s1][s2]=true;
                        in[s2]++;
                    }
                    goto ok;
                }
                if(str[i-1][len]) flag=false;
                ok:;
            }
        }
        if(!flag) goto failed;
        while(!q.empty()) q.pop();
        for(int i=0; i<26; i++){
            if(in[i]==0){
                q.push(i);
            }
        }
        cnt=0;
        while(!q.empty()){
            int now=q.front(); q.pop();
            for(int i=0; i<26; i++){
                if(used[now][i]){
                    in[i]--;
                    if(in[i]==0){
                        q.push(i);
                    }
                }
            }
            cnt++;
        }
        if(cnt!=26) goto failed;
        puts("yes");
        continue;
        failed:;
        puts("no");
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值