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 ...
随机推荐
- Objc中处理数组越界的一种办法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Objc的数组如果在访问时索引非法,则会抛出NSRangeEx ...
- JS滚动显示
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- GrabCut in One Cut(基于图割算法grabcut的一次快速图像分割的OpenCV实现)----目前效果最好的图割
One cut in grabcut(grabcut算法的非迭代实现?) 本文针对交互式图像分割中的图割算法,主要想翻译一篇英文文献.不足之处请大家指正. 这是博主近期看到的效果最好,实现最简单 ...
- FORM中的MOAC控制
1.创建表时,对_ALL表创建同义词 -- Create Multi Org Synonym CREATE OR REPLACE SYNONYM CUX_WF_DEF_HEADER FOR CUX ...
- java中Error与Exception有什么区别
Error类和Exception类都继承自Throwable类. Error的继承关系: java.lang.Object java.lang.Throwable java.lang.Er ...
- Java实现堆的封装,进行插入,调整,删除堆顶以完成堆排序实例
简介 堆对于排序算法是一个比较常用的数据结构,下面我就使用Java语言来实现这一算法 首先,我们需要知道堆的数据结构的形式,其实就是一个特殊的二叉树.但是这个二叉树有一定的特点,除了是完全二叉树以外, ...
- C++ Primer 有感(异常处理)(三)
先看下面的代码: [cpp] view plaincopy int main() { int *i=new int(10); /* 这中间的代码出现异常 */ delete i; return 0; ...
- 手把手教你打造一个心电图效果View Android自定义View
大家好,看我像不像蘑菇-因为我在学校呆的发霉了. 思而不学则殆 丽丽说得对,我有奇怪的疑问,大都是思而不学造成的,在我书读不够的情况下想太多,大多等于白想,所以革命没成功,同志仍需努力. 好了废话不说 ...
- (四十三)UITabBarController和AppDelegate的一些细节
假设一个UITabBar是V1,V2,V3三个视图的根控制器. 一进入程序的时候,只会加载一个视图V1,也就是说UITabBar是延迟加载的. Tip:通过代码创建的UITabBar会一次性加载所有视 ...
- Cocos2D中相关问题提问的几个论坛
如果和SpriteBuilder相关可以到: http://forum.spritebuilder.com 提问. 如果是Cocos2D的问题,则可以到以下论坛询问: http://forum.coc ...