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. UITableView如何撤销移动操作

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道使用UITableView的委托方法canMoveRo ...

  2. Android软件设置自动检查更新

    如果让我推荐功能强大的第三方集成开发包,我一定会推荐友盟,有着强大的软件统计,分析功能(原谅我,我不是打广告). 这一篇介绍友盟的自动更新功能,但是首先你得拥有友盟. 友盟的集成步骤 1.1 导入SD ...

  3. Quick-Cocos2d-X 捋一捋框架流程

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=535 一直比较关注Quick L ...

  4. Java-IO之PrintStream(打印输出流)

    PrintStream是打印输出流,继承于FilterOutputStream,PrintStream是用来装饰其他输出流,为其他输出流添加功能,方便他们打印出各种数据值表示形式.与其他输出流不同,P ...

  5. 【原创】Eclipse vs. IDEA快捷键对比大全

    花了一天时间熟悉IDEA的各种操作,将各种快捷键都试了一下,感觉很是不错!于是就整理了一下我经常用的一些Eclipse快捷键与IDEA的对比,方便像我一样使用Eclipse多年但想尝试些改变的同学们. ...

  6. An universal algorithm design of fixed length substring locating

         An universal algorithm design of fixed length substring locating Stringlocating is a very commo ...

  7. Java-IO之超类InputStream

    InputStream是以字节为单位的输出流,通过以下框架图可以看到InputStream是所有以字节输入流类的公共父类: 基于JDK8的InputStream类源码: public abstract ...

  8. Tomcat中定制阀门

    我们说管道机制给我们带来了更好的扩展性,Tomcat中在扩展性方面具体如何体现,这便是本节讨论的内容.从上节了解到基础阀门是必须执行的,假如你需要一个额外的逻辑处理阀门,可以添加一个非基础阀门. 我的 ...

  9. MacTalk·人生元编程 - 读书笔记

    简介 <MacTalk·人生元编程>是一本随笔文集,主要内容来自作者的微信公众平台"MacTalk By 池建强".本书撰写于2013年,书中时间线却不止于此.作者以一 ...

  10. 【leetcode76】Intersection of Two Arrays II

    题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素累计计数 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: G ...