码迷,mamicode.com
首页 > 编程语言 > 详细

codeforces Gym 100286J Javanese Cryptoanalysis

时间:2015-08-08 22:44:47      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

每一单词相邻两个字母,不能同时为元音或者辅音,一定要有五个元音。。。

各种姿势都可以过:7个for,dp,黑白染色,dfs,并查集。。。。

最主要的思路就是相邻字母连边

见识到了str = str + str2有多慢

#include<bits/stdc++.h>
using namespace std;

#define PB push_back
int color[26];

int G[26][26];
const int maxn  = 1e5+5;
char s[maxn];
bool vis[26];

char mp[26];

bool bipartite(int u)
{
    for(int v = 0; v < 26; v++) if(G[u][v]) {
        if(color[v] == color[u]) return false;
        if(!color[v]){
            color[v] = 3 - color[u];
            if(!bipartite(v)) return false;
        }
    }
    return true;
}


char tab[5] = {A,E,I,O,U};
int tsz = 0,tsz2 = 0;
char tab2[21]  = {B,C,D,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z};

vector<string> word;
vector<int> bcc[26][2];
int bcnt;

bool dfs(int d,int cur,int s)
{
    if(d == bcnt){
        if(cur == 5){
            for(int i = 0; i < bcnt; i++){
               vector<int> &v = bcc[i][(s>>i)&1];
               for(int j = 0; j < v.size(); j++){
                    mp[v[j]] = tab[tsz++];
                }
            }
            return true;
        }
        return false;
    }
    if(dfs(d+1,cur+bcc[d][0].size(),s)||dfs(d+1,cur+bcc[d][1].size(),s|(1<<d))) return true;
    return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    freopen("javanese.in","r",stdin);
    freopen("javanese.out","w",stdout);
    while(~scanf("%s",s)){
        word.PB(s);
        for(int i = 1; s[i]; i++){
            int u = s[i] - A, v = s[i-1] - A;
            G[u][v] =  G[v][u] = 1;
        }
    }

    for(int i = 0; i < 26; i++)if(!color[i]){
        color[i] = 1;
        if(!bipartite(i)) { puts("impossible"); return 0; }
        for(int i = 0; i < 26; i++) if(color[i]&&!vis[i]) {
            vis[i] = true;
            bcc[bcnt][color[i]-1].PB(i);
        }
        bcnt++;
    }

    if(!dfs(0,0,0)) { puts("impossible"); }
    else {
        for(int i = 0; i < 26; i++) if(!mp[i]) mp[i] = tab2[tsz2++];
        for(int j = 0; j < word.size(); j++){
            if(j) putchar( );
            string & str = word[j];
            for(int i = 0; i < str.size(); i++){
                if(A<=str[i]&&str[i]<=Z)
                   putchar(mp[str[i]-A]);
                else putchar(str[i]);
            }
        }
        putchar(\n);
    }
    return 0;
}

 

codeforces Gym 100286J Javanese Cryptoanalysis

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4713981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!