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 ...
随机推荐
- BZOJ【1606】购买干草
1606: [Usaco2008 Dec]Hay For Sale 购买干草 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 859 Solved: 63 ...
- javascript屏蔽脏字
原文发布时间为:2009-04-16 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- hdu 2243 考研路茫茫——单词情结 AC自动机 矩阵幂次求和
题目链接 题意 给定\(N\)个词根,每个长度不超过\(5\). 问长度不超过\(L(L\lt 2^{31})\),只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个? 思路 状态(AC自动 ...
- 键盘事件keydown、keypress、keyup
事件触发顺序:keydown - > keypress - > keyup 中文输入法: firfox:输入触发keydown,回车确认输入触发keyup chrome:输入触发k ...
- 三个div向左浮动不在同一行,向右浮动在同一行的解决办法
前几天在写代码的时候发现了一个问题,问题的大致描述如下: 在一个大的div中,同一行有三个小的div,当三个小的div均向左浮动时,会出现换行问题,均向右浮动时却在同一行. 解决这个问题的方法是在:在 ...
- 学习总结——Postman做http接口功能测试
Postman做各种类型的http接口测试 首先,做接口测试前要有明确的接口文档(e.g. http://test.nnzhp.cn/wiki/index.php?doc-view-59) ,假设已经 ...
- Dialog和软键盘在屏幕上的并存问题:
最近做添加门店,门店昵称自动根据文字变化去搜索后台数据,但是一搜索软键盘就关闭了,感觉用户体验不太好.一开始根本不知道啥问题,找了半天才发现是网络请求dialog加载导致软件盘隐藏的,后面直接把dia ...
- C++ primer分章节快速回顾
第三章: 1,sozeof(int): int n_int=INT_MAX; sizeof n_int;(对变量括号可选) 2,#include<climits>包含一些类型的最大值3,c ...
- Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】
B. Proper Nutrition time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 伪全栈工程师做的有点简陋的ui设计
站酷:http://www.zcool.com.cn/work/ZMjEwMDIxMDA=.html 这个app 叫自我时间管理 是一个 工具 管理自己开会 购物 健身 记账等 的提醒与管理,还可 ...