codeforces Gym 100286J Javanese Cryptoanalysis (二染色)
每一单词相邻两个字母,不能同时为元音或者辅音。。。
各种姿势都可以过:7个for,dp,黑白染色,dfs,并查集。。。。
最主要的思路就是相邻字母连边,把元音和辅音看成两个集合,那么有连边的两个字母一定不能出现在同一个集合中,于是想到了二分图判断的二染色。
比较坑的是,必须要出现5个元音字母
见识到了str = str + str2有多慢
#include<bits/stdc++.h>
using namespace std; #define PB push_back
int color[]; int G[][];
const int maxn = 1e5+;
char s[maxn];
bool vis[]; char mp[]; bool bipartite(int u)
{
for(int v = ; v < ; v++) if(G[u][v]) {
if(color[v] == color[u]) return false;
if(!color[v]){
color[v] = - color[u];
if(!bipartite(v)) return false;
}
}
return true;
} char tab[] = {'A','E','I','O','U'};
int tsz = ,tsz2 = ;
char tab2[] = {'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[][];
int bcnt; bool dfs(int d,int cur,int s)
{
if(d == bcnt){
if(cur == ){
for(int i = ; i < bcnt; i++){
vector<int> &v = bcc[i][(s>>i)&];
for(int j = ; j < v.size(); j++){
mp[v[j]] = tab[tsz++];
}
}
return true;
}
return false;
}
if(dfs(d+,cur+bcc[d][].size(),s)||dfs(d+,cur+bcc[d][].size(),s|(<<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 = ; s[i]; i++){
int u = s[i] - 'A', v = s[i-] - 'A';
G[u][v] = G[v][u] = ;
}
} for(int i = ; i < ; i++)if(!color[i]){
color[i] = ;
if(!bipartite(i)) { puts("impossible"); return ; }
for(int i = ; i < ; i++) if(color[i]&&!vis[i]) {
vis[i] = true;
bcc[bcnt][color[i]-].PB(i);
}
bcnt++;
} if(!dfs(,,)) { puts("impossible"); }
else {
for(int i = ; i < ; i++) if(!mp[i]) mp[i] = tab2[tsz2++];
for(int j = ; j < word.size(); j++){
if(j) putchar(' ');
string & str = word[j];
for(int i = ; i < str.size(); i++){
if('A'<=str[i]&&str[i]<='Z')
putchar(mp[str[i]-'A']);
else putchar(str[i]);
}
}
putchar('\n');
}
return ;
}
codeforces Gym 100286J Javanese Cryptoanalysis (二染色)的更多相关文章
- Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力
原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- NOIP2010关押罪犯 二分+二染色
这个题一上来 没有思路,后来想没有思路就二分吧 那么我们来二分 首先,大于当前的mid值的关系,不能出现在一个集合里 (即关系形成的图是一个二分图,判定二分图可以二染色) 如果不能形成二分图,那么说明 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- hdu 5285 wyh2000 and pupil(二染色)
第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...
随机推荐
- C++11/14的新特性——更简洁
新的字符串表示方式——原生字符串(Raw String Literals) C/C++中提供了字符串,字符串的转义序列,给输出带来了很多不变,如果需要原生义的时候,需要反转义,比较麻烦. C++提 ...
- Unity命令行打包
http://www.66acg.com/?post=137 补充 unity编辑器端获取打包命令行自定义参数,这个可以获取到所有打包时的参数 string[] runArgs = System.En ...
- pytest框架(五)
代码示例一 # coding=utf-8 import pytest @pytest.fixture() def login(): print("输入账号,密码先登录") def ...
- C# 主要运算符中的成员访问(?.)
在开发过程中,我遇到了一种null 条件成员访问的写法,开始不太理解,之后专门查了微软的官方文档,下面是具体内容: 三种成员访问的三种形式 (1)x.y:成员访问. (2)x?.y:null 条件 ...
- UVALive - 3695 Distant Galaxy
InputThere are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ ...
- MySQL server has gone away和Maximum execution time of 120 seconds exceeded
今天在写采集时碰到两个问题1.MySQL server has gone away2.Maximum execution time of 120 seconds exceeded 采集程序写好运行大概 ...
- [题解](组合数学/gcd)luogu_P3166数三角形
首先转化为ans=所有的组合方式 - 在同一水平/竖直线上 - 在同一斜线上 主要考虑在同一斜线上的情况 首先想到枚举斜率然后在坐标系内平移,以(0,0)为起点,每条线上的点数应该是gcd(x,y)比 ...
- BZOJ3622(容斥+dp)
思路 "恰k个"考虑求至少k.k+1--个容斥 题面说所有数字都不同,可以将所求转化为糖比药多的组数恰为\((n+k)/2\)的方案数 \(f[i][j]\)数组我觉得更好的理解方 ...
- sublime下Docblocker插件自定义配置
1.下载安装Docblocker插件 栏目preferences-->Package Control 输入 Docblocker 搜索插件,单击安装 简单使用: 在函数头部输入: /** 按 ...
- python 5 条件判断
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...