【CoderForces 501B】Misha and Changing Handles vector && pair
2016-09-25 15:07
507 查看
Misha and Changing HandlesTime Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64uSubmit StatusDescriptionMisha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.InputThe first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.Next q lines contain the descriptions of the requests, one per line.Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercaseLatin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handlenew isnot used and has not been used by anyone.OutputIn the first line output the integer n — the number of users that changed their handles at least once.In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old andnew,separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new.You may output lines in any order.Each user who changes the handle must occur exactly once in this description.Sample InputInput
5 Misha ILoveCodeforces Vasya Petrov Petrov VasyaPetrov123 ILoveCodeforces MikeMirzayanov Petya IvanovOutput
3 Petya Ivanov Misha MikeMirzayanov Vasya VasyaPetrov123
代码:
#include<stdio.h>#include<algorithm>using namespace std;#include<string.h>#include<iostream>#include<vector>#include<string>#define f first#define s secondint main(){int q;vector< pair<string,string> >d;string a,b;scanf("%d",&q);while(q--){cin>>a>>b; //必须用c++的输入输出;bool flag=false;//每次循环都置0;for(int i=0;i<d.size();i++){if(d[i].s==a){d[i].s=b;flag=true;}}if(!flag)//如果不可以改名,就把它们形成一组;{d.push_back(make_pair(a,b));}}printf("%d\n",d.size());for(int i=0;i<d.size();i++){cout<<d[i].f<<" "<<d[i].s<<endl;}return 0;}
相关文章推荐
- [转]how to sign an assembly with a strong name & how to create a pair key怎样通过强名给程序集签名以及如何创建钥匙对?
- Configuring Spring Oauth2 with JWT & asymmetric RSA keypair
- HDU 5963 朋友/podru 4000 ga (找规律+map&pair基本用法)
- "Shortest" pair of paths - POJ 3068 费用流
- xcode 4.2 "XCode could not find a valid private-key/certificate pair for this profile" 解决方案
- POJ-3068-"Shortest" pair of paths
- Dictionary、List<KeyValuePair<i>>的组合使用
- G4&Sui老师的pair project(197&)
- poj3068 "Shortest" pair of paths 费用流
- UVa 10382 Watering Grass (贪心&pair使用技巧)
- 关于VS2012使用make_pair编译提示“error C2664: “std::make_pair”: 不能将参数 1 从“int”转换为“int &&””
- C++ std::pair<>
- ICPCCamp 2016 Day1 ftiasch's Contest #4(All Pair Shortest Path-位运算)
- bzoj 1786 && bzoj 1831: [Ahoi2008]Pair 配对(DP)
- Pair Project: 电梯调度问题API设计(by 谢俊源 & 陆元伟)
- CF 317A(Perfect Pair-广义Fib序列p,q=1性质2&加法增长极)
- Android Post参数提交 List<NameValuePair>,web端获取数据
- KeyValuePair<TKey, TValue>泛型用法
- pair & make_pair
- C# KeyValuePair<TKey,TValue>的用法【转】