poj2021 字符串处理 BFS

Relative Relatives

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3430   Accepted: 1504

Description

Today is Ted‘s 100th birthday. A few weeks ago, you were selected by the family to contact all of Ted‘s descendants and organize a surprise party. To make this task easier, you created an age-prioritized list of everyone descended from Ted. Descendants of the
same age are listed in dictionary order.

The only materials you had to aid you were birth certificates. Oddly enough, these birth certificates were not dated. They simply listed the father‘s name, the child‘s name, and the father‘s exact age when the baby was born.

Input

Input to this problem will begin with line containing a single integer n indicating the number of data sets. Each data set will be formatted according to the following description.

A single data set has 2 components:

  1. Descendant Count - A line containing a single integer X (where 0 < X < 100) indicating the number of Ted‘s descendants.
  2. Birth Certificate List - Data for X birth certificates, with one certificate‘s data per line. Each certificate‘s data will be of the format "FNAME CNAME FAGE" where: 
    • FNAME is the father‘s name.
    • CNAME is the child‘s name.
    • FAGE is the integer age of the father on the date of CNAMEs birth.

Note:

  • Names are unique identifiers of individuals and contain no embedded white space.
  • All of Ted‘s descendants share Ted‘s birthday. Therefore, the age difference between any two is an integer number of years. (For those of you that are really picky, assume they were all born at the exact same hour, minute, second, etc... of their birth
    year.)
  • You have a birth certificate for all of Ted‘s descendants (a complete collection).

Output

For each data set, there will be X+1 lines of output. The first will read, "DATASET Y", where Y is 1 for the first data set, 2 for the second, etc. The subsequent X lines constitute your age-prioritized list of Ted‘s descendants along with their ages using
the format "NAME AGE". Descendants of the same age will be listed in dictionary order.

Sample Input

2
1
Ted Bill 25
4
Ray James 40
James Beelzebub 17
Ray Mark 75
Ted Ray 20

Sample Output

DATASET 1
Bill 75
DATASET 2
Ray 80
James 40
Beelzebub 23
Mark 5

这题自己感觉也不难,编译成功提交后老是不过,写了几个版本的。后来看了别人AC的代码,发现自己想多了。今晚重改后一次AC.当然这题有多种写法。dfs、用stl里的list写成bfs都可以

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
typedef struct{
	char name[20];
	int age;
}Name;
typedef struct{
	char father[20];
	char son[20];
	int fage;
}Origin;
typedef map<string,int> record;
int n,k,i,j,len,x;
string a,b;
bool cmp(const Name& a,const Name& b){
	if(a.age>b.age)return true;
	if(a.age==b.age&&strcmp(a.name,b.name)<0)return true;
	return false;
}
int main(){
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>x;
		record re;
		string s="Ted";
		re[s]=100;
		len=0;
		Origin ori[100];
		Name name[100];
		for(j=0;j<x;j++){
			cin>>ori[j].father>>ori[j].son>>ori[j].fage;
			if(re[ori[j].father]&&!re[ori[j].son]){
				re[ori[j].son]=re[ori[j].father]-ori[j].fage;
				len++;
			}
		}
		while(len<x){
			for(j=0;j<x&&len<x;j++){
			   if(re[ori[j].father]&&!re[ori[j].son]){
				re[ori[j].son]=re[ori[j].father]-ori[j].fage;
				len++;
			   }
		    }
		}
		for(j=0;j<x;j++){
			strcpy(name[j].name,ori[j].son);
			name[j].age=re[ori[j].son];
		}
		sort(name,name+x,cmp);
		cout<<"DATASET "<<i<<endl;
		for(j=0;j<x;j++)cout<<name[j].name<<" "<<name[j].age<<endl;
	}
	return 0;
}

poj2021 字符串处理 BFS

时间: 2024-08-05 08:41:09

poj2021 字符串处理 BFS的相关文章

Codeforces Round #275 (Div.1) Solution

好久没做题了,开场Virtual热热身. A 构造,我的方法是,取1,2,3...,k这几个差值,前k+1个数分别是 1, k+1, 2, k, ...., 之后就k+2, k+3, ..., n B 因为题设是与操作.我们按照每一位来,如果有一个限制某位是1,则将那段区间标志1,没有限制的位全部置零即可,然后检验为0的位是否全是1.标志一段区间可以用标记法,检验可以求和看差值. C 我做完之后看了CF tutorial 跟我的做法不同.我的做法比他给的复杂度低一点,不过题解好帅,而且跑出来速度

【字符串+BFS】Problem 7. James Bond

https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个这样的串s,s可以由不同的串首尾相连得到 最多100个字符串,所有字符串的总长度不超过5000 [样例解释] aB5可以由a+B5得到,也可以由aB+5得到,所以输出YES [思路] 首先一定是在100个选择2个串a,b,a是b的前缀 然后a和b的前缀可以消去,我们想知道b剩下的右半部分是哪一个串的

【63测试20161111】【BFS】【DP】【字符串】

第一题: tractor 题目描述 农场上有N(1 <= N <= 50,000)堆草,放在不同的地点上.FJ有一辆拖拉机,也在农场上.拖拉机和草堆都表示为二维平面上的整数坐标,坐标值在1..1000的范围内.拖拉机的初始位置与所有草堆不同. FJ开拖拉机时,只能平行于坐标轴(即东.南.西.北四个方向),而且每次开动的一段必须是整数长度. 例如,他可以向北开2个单位长度,然后向东开3个单位长度.拖拉机不能开到草堆的位置. 请帮助FJ计算出最少要移动多少个草堆,他才能将拖拉机开回坐标原点. 拖拉

UVA 10941 - Words adjustment(BFS+字符串处理)

UVA 10941 - Words adjustment 题目链接 题意:给定两个字符串,在给定一些单词集合,问能否两个单词后面各添加一些单词,使得两个单词变成相同,问添加单词最少几次,单词要来自单词集合 思路:广搜,记录状态为两个字符串之间差的字符,利用set和string去乱搞..即可 代码: #include <cstdio> #include <cstring> #include <string> #include <iostream> #inclu

UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子

[coci2012]覆盖字符串 AC自动机

给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串.现在Mirko要用这M个字符串去覆盖给出的那个字符串的.覆盖时,必须保证:1.Mirko的字符串不能拆开,旋转:2.Mirko的字符串必须和给出的字符串的某一连续段完全一致才能覆盖,3.若干次覆盖可以部分重叠4.Mirko的字符串可以无限使用.求给出的字符串当中,有多少个字母是无法覆盖的. 小朋友们,作为一名长者,我认为我有必要向你们传授一些人生的经验~: 字符串的一堆函数,慎用慎用: 本人只因没有仔细认真,把strlen(

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square

HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过,而且也不好控制字典序 换个角度想,虽然起始状态有很多,但是到底哪一位是1,哪一位是2不是最重要的,最重要的是和目标状态对应,所以可以把起始状态重新编码为"12345678"这种形式(先不考虑X),然后目标状态也对应过去,当考虑X的时候,我们可以认为起始状态只有9种,分别是'X'在各个位置的

hdu 3247 Resource Archiver(AC自动机+BFS+DP)

题目链接:hdu 3247 Resource Archiver 题目大意:给定N个需要包含的串,M个不能包含的串,问说满足的最短字符串长度. 解题思路:直接对所有串建立AC自动机,不能满足的串用同一种标记即可.然后处理出所有属于需要包含串的单词节 点,用BFS处理出两两之间的距离,并且过程中是不能经过禁止节点.这样做的原因是节点的个数很多,如果对所有的 节点进行dp的话空间都不够.剩下的就是dp了. #include <cstdio> #include <cstring> #inc