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 ...
随机推荐
- Linux系统编程---守护进程
守护进程是什么?就是在后台运行的进程. 那么如何创建守护进程呢? 1. 创建孤儿进程 2. setsid() 创建进程会话 3. 重定向标准输入, 标准输出 4. chdir, 改当当前进程的工作目录 ...
- VS2010 express中改变VC Default include/lib/… 目录
转自: Liz's Blog http://www.cnblogs.com/lizmy/archive/2012/01/10/2318258.html 2010中是以工程为单位,更改VC++ dire ...
- 4、Android UI测试
为你的APP进行UI测试是为了确保不出现意料之外的结果,提升用户的体验.如果你需要验证你的APP UI的正确性,你需要养成创建UI测试的习惯. Espresso测试框架是由Android Testin ...
- SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式
这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...
- iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...
- Android进阶(二)https请求No peer certificate的解决方法.
在做Android客户端通过https协议访问12306,并爬取数据时,出现了如下错误: 其中有一条错误提示是 javax.net.ssl.SSLPeerUnverifiedException: No ...
- EBS DBA指南笔记(三)
第五章 patching patch的作用:解决应用代码的问题:安装新的特征:更新technology stack组件.打patch不是一个简单的过程,但我们也没必要深究里面每个细节. EBS的p ...
- android studio编译慢的问题
1.修改android studio的使用堆内存,根据自己电脑的内存,尽量设置的大一点,点击help->如下图: 2.接下来设置使用离线gradle构建,一开始就是使用了内置的默认路径gradl ...
- org.apache.poi.ss.usermodel 类操作excel数据遗漏
直接上图. 错误程序: 循环读取每一行的单元格数据部分 //for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) ...
- JSP编译成Servlet(五)JDT Compiler编译器
通过JSP编译器编译后生成了对应的java文件,接下去要把Java文件编译成class文件.对于这部分完全没有必要重新造轮子,常见的优秀编译工具有Eclipse JDT Java编译器和Ant编译器. ...