Codeforces 467D Fedor and Essay bfs
题目链接:
题意:
给定n个单词。
以下有m个替换方式。左边的单词能变成右边的单词。
替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短
输出字母r的个数和单词长度。
思路:
我们觉得一个单词有2个參数。则m个替换规则能够当成m个点的有向图。
则某些单词的替换终点会确定,所以反向建图bfs一下。
为了防止某些点被重复更新,所以把每一个点的权值都放到栈里排个序然后bfs。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<string>
using namespace std; typedef int ll;
const int inf = 1000000;
#define N 300005
int n, m;
void go(string &x){
int len = x.length();
for(int i = 0; i < len; i++)
if('A' <= x[i] && x[i] <= 'Z')
x[i] = x[i]-'A'+'a';
}
string s[N], a[N], b[N];
int S[N], A[N], B[N], LEN[N], NUM[N];
map<string, int> mp;
struct Edge{
int to, nex;
}edge[N*10];
int head[N], edgenum;
void init(){memset(head, -1, sizeof head); edgenum = 0;}
void add(int u, int v){
Edge E = {v, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
struct node{
int x, y, point;
node (int X = 0, int Y = 0, int P = 0):x(X), y(Y), point(P){}
bool operator<(const node&D){
if(D.x!=x)return D.x > x;
return D.y>y;
}
}st[N], dis[N];
int top;
bool cmp(node X, node Y){
if(X.x != Y.x) return X.x < Y.x;
if(X.y != Y.y) return X.y < Y.y;
return X.point < Y.point;
}
int tot;
void BFS(int x){
queue<int>q;
q.push(x);
while(!q.empty()){
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].nex){
int v = edge[i].to;
if(dis[u] < dis[v])
{
dis[v] = dis[u];
q.push(v);
}
}
}
}
void input(){
mp.clear();
init();
top = tot = 0;
for(int i = 1; i <= n; i++)
{
cin>>s[i]; go(s[i]);
if(mp.count(s[i])==0)
{
S[i] = mp[s[i]] = ++tot;
int len = s[i].length(), num = 0;
for(int j = 0; j < len; j++) num += s[i][j]=='r';
st[top++] = node(num, len, tot);
LEN[tot] = len; NUM[tot] = num;
}
else S[i] = mp[s[i]];
}
scanf("%d", &m);
for(int i = 1; i <= m; i++)
{
cin>>a[i]>>b[i]; go(a[i]); go(b[i]);
if(mp.count(a[i])==0)
{
A[i] = mp[a[i]] = ++tot;
int len = a[i].length(), num = 0;
for(int j = 0; j < len; j++) num += a[i][j]=='r';
st[top++] = node(num, len, tot);
LEN[tot] = len; NUM[tot] = num;
}
else A[i] = mp[a[i]];
if(mp.count(b[i])==0)
{
B[i] = mp[b[i]] = ++tot;
int len = b[i].length(), num = 0;
for(int j = 0; j < len; j++) num += b[i][j]=='r';
st[top++] = node(num, len, tot);
LEN[tot] = len; NUM[tot] = num;
}
else B[i] = mp[b[i]];
add(B[i], A[i]);
}
}
int main(){
while(~scanf("%d", &n)){
input();
for(int i = 1; i <= tot; i++)dis[i] = node(NUM[i], LEN[i],0);
sort(st, st+top, cmp);
for(int i = 0; i < top; i++)
BFS(st[i].point);
long long ans1 = 0, ans2 = 0;
for(int i = 1; i <= n; i++)
ans1 += dis[S[i]].x, ans2 += dis[S[i]].y; printf("%I64d %I64d\n", ans1, ans2);
}
return 0;
}
Codeforces 467D Fedor and Essay bfs的更多相关文章
- CodeForces 467D(267Div2-D)Fedor and Essay (排序+dfs)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF467D Fedor and Essay 建图DFS
Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...
- Codeforces 467D
题目链接 D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【Codeforces 467D】Fedor and Essay
Codeforces 467 D 题意:给\(m\)个单词,以及\(n\)个置换关系,问将\(m\)个单词替换多次后其中所含的最少的\(R\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
题意:给一篇文章,再给一些单词替换关系a b,表示单词a可被b替换,可多次替换,问最后把这篇文章替换后(或不替换)能达到的最小的'r'的个数是多少,如果'r'的个数相等,那么尽量是文章最短. 解法:易 ...
- [CF467D] Fedor and Essay
After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying com ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
随机推荐
- 完美CSS文档的8个最佳实践
在css的世界,文档没有被得到充分的利用.由于文档对终端用户不可见,因此它的价值常常被忽视.另外,如果你第一次为css编写文档,可能很难确定哪些内容值得记录,以及如何能够高效完成编写. 然而,为C ...
- Qualcomm download 所需要的 contents.xml
Platform MSM8917 PM8937 PMI8940 在 Qualcomm code base 中, amss下有許多 MSM89xx 之類的 folder, 這些是為了不同 chip 所產 ...
- Linux:supervisor命令的使用
supervisor是Linux下一个便利的启动和监控服务的命令. 举例来说:假如我想同时管理一堆的服务,包括他们的运行和停止.我就可以使用supervisor来管理. supervisor包括两 ...
- Python Challenge 第十四关
14关页面上是两张图,一张是一个卷面包,一张类似条形码的东西.没任何提示,就看源代码,果然,有一行注释: <!-- remember: 100*100 = (100+99+99+98) + (. ...
- nodejs后台启动
可避免关闭窗口,程序就关闭,可在后台运行 安装forever包,一般用于服务器,调试环境可不安装 npm install forever -g 启动方式如图: 查询后台运行哪些程序 forever l ...
- 项目中使用libsodium扩展
前段时间研究了微信小微商户,地址:https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=19_11 其接口操作中需要下载证书针对返回的密 ...
- Mycat 做简单的读写分离(转载)
大漠小狼的个人空间 http://www.51testing.com/html/34/369434-3686088.html 使用Mycat 做简单的读写分离(一) 原本使用的是amoeba做的读 ...
- [读书笔记] JavaScript设计模式: 单例模式
单例模式:保证一个类只有一个实例,并提供一个可以访问它的全局访问点. 一种简单.方便的写法就是用一个变量来标识当前类是否已经创建过对象,如果有,则返回已经创建好的对象,否则创建一个新对象,并将其返回. ...
- IntelliJ IDEA中日志分类显示设置
说明:很遗憾,IDEA中无法实现日志分类查看,比如只能显示INFO级别的,但是可以有搜索功能[Ctrl]+[F].好像找不到好用的插件,Andorid Studio好像有一个插件可以. 解决方式: 直 ...
- dump_stack的简单使用
转载:http://blog.csdn.net/sanchuyayun/article/details/39183941 刚刚接触内核,在调试过程中用printk打印信息当然是直接有效的办法,但当我们 ...