1032 Sharing (25分)

题目

思路

定义map存储所有的<地址1,地址2>

第一set存放单词1的所有地址(通过查找map)

通过单词二的首地址,结合map,然后在set中查找是否存在,如果存在就是所求的地址,没有就是-1

注意点

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
using namespace std; int main() {
int n;
string a, b;
cin>>a>>b>>n;
map<string, string> common;
set<string> c1;
for(int i=0;i<n;i++){
string s1, s2;
char ch;
cin>>s1>>ch>>s2;
common[s1] = s2;
}
string _a = a;
c1.insert(_a);
while((_a = common[_a]) != "-1"){
c1.insert(_a);
}
c1.insert(_a); string _b = b;
if(c1.find(_b) != c1.end()){
printf("%s", _b.c_str());
return 0;
}
while((_b = common[_b]) != "-1"){
if(c1.find(_b) != c1.end()){
printf("%s", _b.c_str());
return 0;
}
}
printf("-1");
}

1032 Sharing (25分)的更多相关文章

  1. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  2. 【PAT甲级】1032 Sharing (25 分)

    题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...

  3. 1032 Sharing (25分)(数组链表)

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  4. PAT 1032 Sharing (25分) 从自信到自闭

    题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...

  5. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  6. PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏

    1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...

  7. 1032. Sharing (25) -set运用

    题目如下: To store English words, one method is to use linked lists and store a word letter by letter. T ...

  8. 1032. Sharing (25)

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  9. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

随机推荐

  1. 剑指offer-面试题24-反转链表-链表

    /* 题目: 定义一个函数,输入链表的头结点,反转链表输出反转后链表的头节点. */ /* 思路: 记录当前节点的next和pre. 断开当前节点指向next的指针,指向pre. */ #includ ...

  2. 产生随机数(rand()函数和srand()函数)

    有时候,我们需要随机产生一个在某范围的数,C/C++提供了一个库函数rand()来产生随机数. 函数原型:int rand(void); 功能:返回一个[0,RAND_MAX]间的随机整数.其中RAN ...

  3. java9小工具jshell

    1.jshell是jdk9引入的小工具 2.启动jshell 在命令行输入jshell 3.使用jshell 比如定义a=10;b=20;输出a+b的结果,有如下两种方法 方法1:代码写在一行,回车直 ...

  4. PTA 1002 A+B for Polynomials

    问题描述: This time, you are supposed to find A+B where A and B are two polynomials. Input Specification ...

  5. git配置多仓库

    git配置多仓库 github , gitee , coding , gitlab , gitlab.company ..... 真TM多 . 真TM多 . 真TM多 . 生成ssh 生成ssh 密钥 ...

  6. ubuntu设置ulimit

    centos系统的设置ulimit的时候是直接修改/etc/security/limits.conf文件,但是在ubuntu中却不行, ubuntu先修改/etc/security/limits.co ...

  7. 850. Dijkstra求最短路 II(堆优化模板)

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为非负值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行 ...

  8. Vue前端挂载对象时一些思考

    最近,在Vue前端调试http请求,无论如何如何也是拦截不了某些http请求.场景是这样的:Java后端组装好Vue对象,然后送到前端,前端通过id来挂载该Vue对象,而该对象中有上传文件或者图片的控 ...

  9. LAMP+discuz网站搭建过程

    LAMP+discuz网站的搭建 一. LAMP环境搭建 0x01下载配置虚拟机 网上下载centOS7 64的镜像,然后在vmware里面配置好,我配置的是linux终端桌面,运行快,占内存小. 0 ...

  10. c++中sort基础用法

    用法一:数组排序 对一个数组进行升序排序 #include <algorithm> #include <iostream> #include <cstdio> us ...