2018 ACM-ICPC World Finals B.Comma Sprinkler
WF里面最简答一题,就是一个dfs就可以了,已经访问过的点可以不再访问
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int N = 1000005;
char s[1000005];
map<string, int> StoI;
vector<string> ItoS;
int tot = 0;
vector<int> result;
struct Node {
int to, nx;
} E[N][2];
int head[N][2];
int tol[2] = {0, 0};
int tag[N][2];
int vis[N][2];
void add(int fr, int to) {
E[tol[0]][0].to = to;
E[tol[0]][0].nx = head[fr][0];
head[fr][0] = tol[0]++;
E[tol[1]][1].to = fr;
E[tol[1]][1].nx = head[to][1];
head[to][1] = tol[1]++;
}
void dfs(int x, int flag) {
vis[x][flag] = 1;
tag[x][flag]++;
// printf("%d %d %s\n", x, flag, ItoS[x].c_str());
for (int i = head[x][flag]; ~i; i = E[i][flag].nx) {
int to = E[i][flag].to;
if (vis[to][flag ^ 1])
continue;
dfs(to, flag ^ 1);
}
}
int main() {
#ifdef LOCAL
freopen("/Users/basasuya/ACM/in.txt", "r", stdin);
#endif
int cnt = 0;
memset(head, -1, sizeof(head));
while (1) {
s[cnt++] = getchar();
if (s[cnt - 1] == -1 || s[cnt - 1] == '\n') {
s[cnt - 1] = 0;
break;
}
}
int sLen = strlen(s);
string tmp;
ItoS.push_back(".");
for (int i = 0; i < sLen; ++i) {
if (s[i] == ' ' || s[i] == '.' || s[i] == ',') {
if (!tmp.empty()) {
if (StoI.find(tmp) == StoI.end()) {
++tot;
StoI[tmp] = tot;
ItoS.push_back(tmp);
}
int Id = StoI[tmp];
result.push_back(Id);
}
if (s[i] == '.')
result.push_back(0);
else if (s[i] == ',')
result.push_back(-1);
tmp.clear();
} else
tmp += s[i];
}
for (int i = 0, len = result.size(); i < len; ++i) {
if (i >= 1 && result[i - 1] > 0 && result[i] > 0) {
add(result[i - 1], result[i]);
} else if (i >= 2 && result[i - 2] > 0 && result[i] > 0 && result[i - 1] == -1) {
add(result[i - 2], result[i]);
tag[result[i - 2]][0]++;
tag[result[i]][1]++;
}
}
for (int i = 1; i <= tot; ++i) {
if (!vis[i][0] && tag[i][0]) {
dfs(i, 0);
}
if (!vis[i][1] && tag[i][1]) {
dfs(i, 1);
}
}
for (int i = 0, len = result.size(), ok = 0; i < len; ++i) {
if (i && result[i - 1] > 0 && result[i] > 0 && (tag[result[i - 1]][0] || tag[result[i]][1]))
printf(",");
if (result[i] < 0)
printf(",");
else if (result[i] == 0)
printf(".");
else {
if (i)
printf(" ");
string& tmp = ItoS[result[i]];
for (int j = 0, len = tmp.length(); j < len; ++j) {
printf("%c", tmp[j]);
}
}
}
}
2018 ACM-ICPC World Finals B.Comma Sprinkler的更多相关文章
- ACM - ICPC World Finals 2013 C Surely You Congest
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 试题来源 ACM/ICPC World Fin ...
- ACM - ICPC World Finals 2013 A Self-Assembly
原题下载 : http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这道题其实是2013年我AC的第一道题,非常的开心,这 ...
- ACM - ICPC World Finals 2013 F Low Power
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 有n个机器,每个机器有2个芯片,每个 ...
- ACM - ICPC World Finals 2013 I Pirate Chest
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 海盗Dick受够了在公海上厮杀.抢劫 ...
- ACM - ICPC World Finals 2013 H Матрёшка
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...
- ACM - ICPC World Finals 2013 D Factors
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 一个最基本的算数法则就是大于1的整数 ...
- ACM - ICPC World Finals 2013 B Hey, Better Bettor
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这题真心的麻烦……程序不长但是推导过程比较复杂,不太好想 ...
- [算法竞赛入门经典]Message Decoding,ACM/ICPC World Finals 1991,UVa213
Description Some message encoding schemes require that an encoded message be sent in two parts. The ...
- UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列
Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but ...
随机推荐
- iOS下JS与OC互相调用(一)--UIWebView 拦截URL
最近准备把之前用UIWebView实现的JS与原生相互调用功能,用WKWebView来替换.顺便搜索整理了一下JS 与OC 交互的方式,非常之多啊.目前我已知的JS 与 OC 交互的处理方式: * 1 ...
- (一〇四)使用Xcode6创建framework动态静态库
在Xcode6以前,创建framework可以使用iOS-Universal-Framework模板来创建framework,现在苹果已经提供了模板,如下图选择: 使用此模版创建的默认是动态库,方法和 ...
- (NO.00004)iOS实现打砖块游戏(十三):伸缩自如,我是如意金箍棒(下)!
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 准备缩短反弹棒素材 和上一篇类似,我们如法炮制一张缩短后反弹棒的 ...
- 程序员高效Windows环境配置
个人比较追求高效.效率.以下是我常用的windows配置希望对大家有帮助.(身为程序员,我特别喜欢mac pro的retina屏,在那编程简直是一种享受.等我买了mac pro在发一篇 ...
- HTML中锚点的使用
说到锚点,我们也许会需要稍微的思考一下,什么是锚点? 官方的答案是这样的: 那么你可能就会有下面的疑问,锚点能做什么啊? 回答就是,锚点可以理解为一个标记,一个用于而且便于寻找的标记.常用于网页制作的 ...
- SendMessageUpwards定义简单按钮(Unity3D开发之十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=582 SendMessageUp ...
- 【TCP/IP 协议】 TCP/IP 基础
总结 : 通过学习 TCP/IP 基础, 并总结相关笔记 和 绘制思维导图 到博客上, 对 TCP/IP 框架有了大致了解, 之后开始详细学习数据链路层的各种细节协议, 并作出笔记; 博客地址 : h ...
- 02_Weblogic课程之安装篇:RedHat下JDK安装,RedHat下Weblogic安装,目录结构,环境变量设置
1 Weblogic的安装方式有三种: 一.GUI方式安装 (java –jar wls1035_generic.jar [-mode=gui])这是默认的 二.Console方式安装 ...
- EFI怎么装系统? UEFI BIOS
关于EFI的介绍,就不赘述了. 大家可以看看这个帖子 http://benyouhui.it168.com/thread-2488583-1-1.html 总之,新电脑都是这玩意,win8也做了相应E ...
- Java 8新特性探究(二)类型注解和重复注解
本文将介绍java 8的第二个特性:类型注解. 注解大家都知道,从java5开始加入这一特性,发展到现在已然是遍地开花,在很多框架中得到了广泛的使用,用来简化程序中的配置.那充满争议的类型注解究竟是什 ...