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 ...
随机推荐
- c#.net用JavaScript实现 时钟显示
原文发布时间为:2008-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 显示日期,星期,以及时间: JS文件夹与default.aspx在同一个文件夹下 JS文件夹中有date.js文件 ...
- Dependency Injection in ASP.NET Web API 2
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- C# 时间戳和普通时间相互转换
// 时间戳转为C#格式时间 private DateTime StampToDateTime(string timeStamp) { DateTime dateTimeStart = TimeZon ...
- 设置自定义Dialog背景不变暗
设置Dialog弹窗的背景不变暗,有两种方式,一种是通过在style中设置,一种是通过代码设置. 一.在style中设置 <style name="dialog_waiting&quo ...
- C++ 11中几个我比较喜欢的语法(二)
之前在文章C++ 11中几个我比较喜欢的语法中介绍了几个我比较喜欢的C++语法,其中有些语法由于VC 11还不支持,无法跨平台,所以没有介绍.前几天VS 2013 Preview发布后,对C++ 11 ...
- weblogic多池与oracle集群RAC
http://www.techpaste.com/2013/04/soa-infra-start-fails-weblogic-common-resourceexception-good-connec ...
- 11G在用EXP导出时,空表不能导出
11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...
- java基础篇4之注解
1 注解的应用(jdk1.5的新特性) 一个注解相当于一个特殊的类 例子: @SuppressWarning("deprecation") @Deprecated @Overrid ...
- 【音乐App】—— Vue-music 项目学习笔记:歌曲列表组件开发
前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 当前歌曲播放列表 添加歌曲 ...
- 【重点突破】——第三方绘图工具FusionCharts.js的使用详解
一.引言 项目组中,经常会因为绘制图表的繁杂度,衡量会不会使用第三方绘图工具,如果自己做很困难,成本使用高于第三方绘图工具库,就会使用.很多人使用的是Chart.js,因为它是免费使用的,不过,缺点就 ...