zoukankan      html  css  js  c++  java
  • POJ 1270

    #include<iostream>
    #include<algorithm>
    #define MAXN 26
    #define MAX 300
    using namespace std;
    
    bool floy[MAXN][MAXN];
    char var[MAX];
    char cons[MAX];
    int a[MAXN];
    void tran_var();
    void give_floy();
    bool is_ok();
    void floyd();
    int main()
    {
        //freopen("acm.acm","r",stdin);
        int len;
        while(gets(var))
        {
            memset(floy,false,sizeof(floy));
            memset(a,0,sizeof(a));
            gets(cons);
            tran_var();
        //    cout<<var<<endl;
            give_floy();
            floyd();
            len = strlen(var);
        //    cout<<len;
            sort(var,var + len);
            do
            {
                //cout<<"0000000000"<<endl;
                if(is_ok())
                    cout<<var<<endl;
            }while(next_permutation(var,var+len));
            cout<<endl;
        }
    }
    
    void tran_var()
    {
        int i;
        int j;
        int len = strlen(var);
        for(i = 0,j = 0; i < len; i += 2)
        {
            var[j] = var[i];
            ++ j;
        }
        var[j] = '';
    }
    
    void give_floy()
    {
        int i;
        int j;
        int len = strlen(cons);
        for(i = 0; i < len; i += 4)
        {
            floy[cons[i] - 'a'][cons[i+2] - 'a'] = true;
        }
    //    floyd();
    }
    
    void floyd()
    {
        int i;
        int j;
        int k;
        for(k = 0; k < MAXN; ++ k)
        {
            for(i = 0; i < MAXN; ++ i)
            {
                for(j = 0; j < MAXN; ++ j)
                {
                    if(floy[i][k] && floy[k][j])
                    {
                        floy[i][j] = true;
                    }
                }
            }
        }
    }
    
    bool is_ok()
    {
        int i;
        int j;
        int len = strlen(var);
    //    cout<<len<<endl;
        for(i = 0; i < len; ++ i)
        {
            a[var[i] - 'a'] = i;
        }
    //    for(i = 0; i < len; ++ i)
    //    {
    //        cout<<a[i]<<" ";
    //    }
    //    cout<<endl;
        for(i = 0; i < MAXN; ++ i)
        {
            for(j = 0; j < MAXN; ++ j)
            {
                if(floy[i][j] && a[i] > a[j])
                    return false;
            }
        }
        return true;
    }

    关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

    技术网站地址: vmfor.com

  • 相关阅读:
    Python os.getcwd()方法
    Python os.walk()方法
    PyTorch 模型构造
    Python map()函数
    字符串转数字测试--知识备忘
    如何判断一个变量是数组Array类型--实例--加个人见解
    js面试题
    ios学习笔记
    读取图片文件--实例
    名言记录
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4563350.html
Copyright © 2011-2022 走看看