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 ...
随机推荐
- 2010年最佳jQuery插件
原文发布时间为:2011-01-19 -- 来源于本人的百度文章 [由搬家工具导入] WDL的作者从大量的优秀jQuery插件精心筛选出一些对Web Designers有帮助的和具备非常不错的视觉效果 ...
- mdf 与 mdb的对比
下面的内容从网上搜索而来,未经过本人严格验证,仅供参考. 1.问:mdb数据库能否脱离Access运行?即,没有安装Access,可以打开mdb吗? 答:可以,脱离Access运行,可以到微软的同类产 ...
- 【原创】Javascript-获取URL请求参数
function getUrlParam() { var param = [], hash; var url = window.location.href;//获取网页的url var hashes ...
- 大型网站优化-memcache技术
大型网站优化-memcache技术 memory+cache 内存缓存 memcache简介 memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发 ...
- MySQL的INFORMATION_SCHEMA数据库简介
大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库.information_schema数据库是做什么用的呢,使用WordPress博客 ...
- [Python Debug] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
I Got a SettingWithCopyWarning when I ran the following code: tmp=date[date['date'].isnull().values= ...
- 提升开发效率的一款mybatis开发神器
文末附有完整案例的代码内容!! 以前在开发的时候,使用mybatis的时候,经常都需要先配置xml映射文件,然后每条sql操作都需要自己进行手动编写,对于一些复杂的sql这么来操作确实有必要,但是如果 ...
- gtest 自动化测试 部署
1.部署 a)编译框架 1.1下载gtest库1.6.0 并解压到文件夹 "/user/{user}/gtest.1.6.0" 下载地址:https://code.google.c ...
- 如何在SQLite中创建自增字段
SQLite 简单的回答:一个声明为 INTEGER PRIMARY KEY 的字段将自动增加. 这里是详细的答案: 从 SQLite 的 2.3.4 版本开始,如果你将一个表中的一个字段声明为 ...
- Scut游戏服务器引擎之新手入门
1. 开发语言:Scut提供C#或Python两种脚本语言开发,Python脚本的性能会比较差,建议使用编译执行的C#代码: 2. 运行平台:Scut可以Window与Linux平台上运行,Linux ...