糯米

TI DaVinci, gstreamer, ffmpeg
随笔 - 167, 文章 - 0, 评论 - 47, 引用 - 0
数据加载中……

POJ 1270 Following Orders 全排列

 

#include <stdio.h>
#include 
<string.h>

char map[26], rev_map[26], res[26];
int N, pre[26];

void dfs(int idx, int used)
{
    
int i;

    
if (idx == N) {
        
for (i = 0; i < N; i++)
            printf(
"%c", map[res[i]] + 'a');
        printf(
"\n");
        
return ;
    }


    
for (i = 0; i < N; i++{
        
if (used & (1 << i))
            
continue;
        
if ((pre[i] & used) != pre[i])
            
continue;
        res[idx] 
= i;
        dfs(idx 
+ 1, used | (1 << i));
    }

}


int main()
{
    
char a, b;
    
int i;

    freopen(
"e:\\test\\in.txt""r", stdin);

    
while (1{

        memset(rev_map, 
0sizeof(rev_map));
        
while (1{
            a 
= getchar();
            
if (a == EOF)
                
return 0;
            
if (a == '\n')
                
break;
            
if (a >= 'a' && a <= 'z'
                rev_map[a 
- 'a'= 1;
        }

        
for (i = N = 0; i < 26; i++)
            
if (rev_map[i]) {
                map[N] 
= i;
                rev_map[i] 
= N++;
            }


        memset(pre, 
0sizeof(pre));
        i 
= 0;
        
while (1{
            a 
= getchar();
            
if (a == '\n' || a == EOF)
                
break;
            
if (a < 'a' || a > 'z'
                
continue;
            
if (i & 1
                pre[rev_map[a 
- 'a']] |= 1 << rev_map[b - 'a'];
            
else
                b 
= a;
            i
++;
        }


        dfs(
00);
        printf(
"\n");
    }


    
return 0;
}

posted on 2010-02-27 12:24 糯米 阅读(266) 评论(0)  编辑 收藏 引用 所属分类: POJ


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理