Codeforces1303C. Perfect Keyboard
本题可以转换成图论来做,每两个相邻点连一条边,然后统计每一个点的degree,如果>=2说明有一个点要相邻三个点,不满足题意,然后找出每个degree<2的点,这些点可以作为一段的起点,然后依次dfs添加,如果26个字母有一个没有dfs到的,就无解
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; char str[];
bool connect[][], vis[];
int degree[];
string ans; void dfs(int u) {
if(vis[u]) return;
vis[u] = true, ans += (char)(u+'a');
for(int i = ; i <= ; ++i)
if(!vis[i] && connect[u][i]) dfs(i);
} void run_case() {
cin >> str;
ans = "";
memset(connect, , sizeof(connect)), memset(degree, , sizeof(degree));
memset(vis, , sizeof(vis));
int siz = strlen(str);
for(int i = ; i < siz-; ++i) {
int a = str[i] - 'a', b = str[i+] - 'a';
connect[a][b] = connect[b][a] = true;
}
for(int i = ; i <= ; ++i)
for(int j = ; j <= ; ++j)
if(connect[i][j]) degree[i]++;
for(int i = ; i <= ; ++i)
if(degree[i] > ) {
cout << "NO\n";
return;
}
for(int i = ; i <= ; ++i)
if(degree[i] < && !vis[i]) dfs(i);
for(int i = ; i <= ; ++i)
if(!vis[i]) {
cout << "NO\n";
return;
}
cout << "YES\n" << ans << "\n";
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(10);
int t; cin >> t;
while(t--)
run_case();
cout.flush();
return ;
}
Codeforces1303C. Perfect Keyboard的更多相关文章
- Educational Codeforces Round 82 C. Perfect Keyboard
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him ...
- [CF1303C] Perfect Keyboard - DFS
Solution 根据原字符串建图,每个字符是一个点,相邻则连边 然后从每一个度数为 \(1\) 的点开始爆搜连通块,合法情况下应该是一条链 #include <bits/stdc++.h> ...
- 「CF1303C Perfect Keyboard」
前置芝士 图的遍历:通过DFS或者BFS遍历全图. 前向星:用来存边,但是在本题用也可以用一个二维数组解决. 具体做法 先从判断YES和NO开始,可以发现如果一个字母与三个及以上不同的字母相邻时肯定是 ...
- Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...
- 【题解】Educational Codeforces Round 82
比较菜只有 A ~ E A.Erasing Zeroes 题目描述: 原题面 题目分析: 使得所有的 \(1\) 连续也就是所有的 \(1\) 中间的 \(0\) 全部去掉,也就是可以理解为第一个 \ ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- Fedora 22中的Locale and Keyboard Configuration
Introduction The system locale specifies the language settings of system services and user interface ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- word写文档体会
1.找一个文档规范要求. 2.根据文档的规范要求调整正文的格式,标题1的格式,标题2的格式,标题3的格式,图表的格式,把没用的那些格式都删除掉. 3.图注表注后空格一行. 4.设置页眉页脚. 5.生成 ...
- Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat
<resultMap id="BaseResultMap" type="com.youotech.tl_cons_credit_rating.entity.TL_C ...
- eclipse 切换主题Theme
eclipse切换主题theme 1. 打开help -> eclipse marketplace,搜索theme 2. 点击右侧安装后一直继续,直到安装完成 3. 安装完成后会重启eclips ...
- [Python] Tkinter的食用方法_01_简单界面
#开始 放假之后感觉整个人已经放飞自我了,完全不知道自己一天天在干什么,明明有很多的事情需要做,但是实际上每天啥都没做,,,虚度光阴... 晚上突然心烦意乱,开始思考今天一天都做了什么,感觉很有负罪感 ...
- 【PAT甲级】1081 Rational Sum (20 分)
题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...
- 广度优先搜索(BFS)与深度优先搜索(DFS)的对比及优缺点
深搜,顾名思义,是深入其中.直取结果的一种搜索方法. 如果深搜是一个人,那么他的性格一定倔得像头牛!他从一点出发去旅游,只朝着一个方向走,除非路断了,他绝不改变方向!除非四个方向全都不通或遇到终点,他 ...
- java处理节假日和工作时间的工具类
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; impo ...
- HDU 2063 过山车(二分图 && 匈牙利 && 最小点覆盖)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 这是一道很经典的匈牙利问题: 把男同学看成左边点,女同学看成右边点,如果两个同学愿意同 ...
- FYF的煎饼果子
利用等差数列公式就行了,可以考虑特判一下m >= n($ m, n \neq 1 $),这时一定输出“AIYAMAYA”. #include <iostream> using nam ...
- linux理论知识点(用于考试)
ps:为其十天左右的linux培训即将结束了,未雨绸缪,为了更好的通过之后的考试,提前多看些考试题和知识点.这是在chinaunix论坛看到的一个帖子,贴来分享. 原文地址:[http://bbs.c ...