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的更多相关文章

  1. ACM - ICPC World Finals 2013 C Surely You Congest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 试题来源 ACM/ICPC World Fin ...

  2. ACM - ICPC World Finals 2013 A Self-Assembly

    原题下载 : http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这道题其实是2013年我AC的第一道题,非常的开心,这 ...

  3. ACM - ICPC World Finals 2013 F Low Power

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 有n个机器,每个机器有2个芯片,每个 ...

  4. ACM - ICPC World Finals 2013 I Pirate Chest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 海盗Dick受够了在公海上厮杀.抢劫 ...

  5. ACM - ICPC World Finals 2013 H Матрёшка

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...

  6. ACM - ICPC World Finals 2013 D Factors

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 一个最基本的算数法则就是大于1的整数 ...

  7. ACM - ICPC World Finals 2013 B Hey, Better Bettor

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这题真心的麻烦……程序不长但是推导过程比较复杂,不太好想 ...

  8. [算法竞赛入门经典]Message Decoding,ACM/ICPC World Finals 1991,UVa213

    Description Some message encoding schemes require that an encoded message be sent in two parts. The ...

  9. UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列

    Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but ...

随机推荐

  1. UNIX网络编程——原始套接字的魔力【下】

    可以接收链路层MAC帧的原始套接字 前面我们介绍过了通过原始套接字socket(AF_INET, SOCK_RAW, protocol)我们可以直接实现自行构造整个IP报文,然后对其收发.提醒一点,在 ...

  2. 编译GDAL支持MySQL

    GDAL支持MySQL需要MySQL的库才可以,编译很简单,修改nmake.opt文件中对应的MySQL的库的路径和lib即可. nmake.opt文件中397行左右,如下: # MySQL Libr ...

  3. android布局##TableLayout和FrameLayout-android学习之旅(十五)

    TableLayout 表格布局 tablelayout简介 表格布局有TableLayout代表,但是它的本质定义仍然是线性管理器.表格布局采用行和列来管理UI,但是不需要明确的定义多少行,多少列, ...

  4. android orm持久层框架

    ; ; i < 2; i++) { )); ); h1.setWord("这是修改过的数据"); tv.setText(tv.getText() + "\n&quo ...

  5. Struts2知识点学习笔记

    写给自己的话 好记性不如烂笔头,不可能说看了一遍视频就可以完全掌握的.留下这篇笔记,便于今后的复习吧. 1. 访问ServletAPI 访问ServletAPI(response,request,)的 ...

  6. HTML5中 HTML格式化/HTML样式/链表/表格-样式 韩俊强的博客

    HTML5学习从简单到复杂,循环渐进! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 1.HTML格式化 <!DOCTYPE html> <h ...

  7. 编译GDAL使用最新的HDF库配置文件

    HDF库最新版本中的动态库以及目录结构都发生了变化,导致按照之前的博客进行编译GDAL时,会出问题.使用HDF4版本为HDF4-4.2.10,HDF5的版本为HDF5-1.8.12.两个库的目录结构如 ...

  8. Hessian探究(一)Hessian与springMVC结合

    上一篇博客Hessian探究(一)Hessian入门示例我们初步简单的介绍了一下Hessian的使用入门示例,我们是通过Servlet来暴露Hessian的对外服务的,接下来我们介绍一下通过Sprin ...

  9. iOS中 自定义系统相机 作者:韩俊强

    需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...

  10. Chipmunk僵尸物理对象的出现和解决(六)

    既然出现了这个问题下面就是如何找到原因. 因为该问题不是每次都出现,偶尔反弹棒碰到五角星时才会多出一个僵尸棒,现象比较随机,较难悉知具体原因. 有时多次触碰又没有出现问题,有时短时间内每次触碰都出现问 ...