浙江大学复试编程题之——Sharing
来源:互联网 发布:大数据专业课程有哪些 编辑:程序博客网 时间:2024/06/06 09:17
题目描述:
To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, "loading" and "being" are stored as showed in Figure 1.
Figure 1 You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1).
输入描述:
For each case, the first line contains two addresses of nodes and a positive N (<= 10^5), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by -1.Then N lines follow, each describes a node in the format:Address Data Nextwhere Address is the position of the node, Data is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, and Next is the position of the next node.
输出描述:
For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output "-1" instead.(题目转自牛客网)
===================================================================================================================
意译:题目给我们两个字符串,为了节省空间,两个字符串有可能后面有交汇的结点,比如:being与playing可以公用ing来节省空间,而结点i就是他 们的第一个交汇结点,现在给我们不超过10万个结点,共可以组成两个字符串,每个结点有下一个结点的地址,与本结点代表字母与本结点地址 的信息。要求我们找出两个字符串中第一个交汇的结点的地址并显示。
输入描述:
第一个串头的地址 第二个串头的地址 结点个数N
第一个结点地址 第一个结点内含字母 相对于第一个结点所在串中位置的下一位置的结点
第二个结点地址 第二个结点内含字母 相对于第二个结点所在串中位置的下一位置的结点
。。。。。。
第N个结点地址 第N个结点内含字母 相对于第N个结点所在串中位置的下一位置的结点
输出描述:
若有交汇结点则输出第一个交汇的结点的地址,若无则输出-1
====================================================================================================================================
#include<iostream>#include<vector>using namespace std;typedef struct node{char data;int Next_Addr; //下一个结点地址 int flag; //访问次数标记 }Node;int Get_Com(vector<Node> &Pre, int h1, int h2){int i,t1,t2; i = h1; while(i != -1){Pre[i].flag += 1;i = Pre[i].Next_Addr; //访问串1的下一个结点 }i = h2;while(i != -1){Pre[i].flag += 1;if(Pre[i].flag == 2)return i;i = Pre[i].Next_Addr;}return -1; //说明所有结点都只有被访问过一次,即无交汇结点,返回-1 }int main(){int N,Addr1,Addr2,temp_Addr;int Com;vector<Node> Pre;while(cin>>Addr1>>Addr2>>N){Pre.clear();Pre.resize(100000);for(int i=0; i<N; i++)Pre[i].flag = 0;for(int i=0; i<N; i++){cin>>temp_Addr;cin>>Pre[temp_Addr].data>>Pre[temp_Addr].Next_Addr;}Com = Get_Com(Pre,Addr1,Addr2);if(Com != -1){ //-1前面不需要补充0 cout.width(5);cout.fill('0');}cout<<Com<<endl;}return 0;}
- 浙江大学复试编程题之——Sharing
- 浙江大学复试真题——1788:A + B
- 浙江大学研究生复试上机真题——1975:ZOJ
- 浙江大学计算机学院2007年考研复试上机题
- 浙江大学2008年考研复试机试第2题
- 浙江大学2008年考研复试机试第3题
- 浙江大学2008年考研复试机试第4题
- 浙江大学2008年考研复试机试第5题
- 浙江大学2011年复试上机题 解题报告
- 浙江大学2010年复试上机题 解题报告
- 浙江大学2007年复试上机题 解题报告
- 浙江大学2006年复试上机题 解题报告
- 浙江大学2005年复试上机题 解题报告
- 来源:2013年浙江大学复试机试模拟题
- pat 1057. Stack (30) 浙江大学复试上机最后一题
- 2011年浙江大学机械复试
- 浙江大学 ACM俱乐部 1994:Sharing
- 2017浙江大学计算机和软件考研复试题A题 N Queens Puzzle
- Ananconda安装Parallel Python(如何在只检测出默认Python27确定默认安装路径的情况安装)
- HeadFirstSQL学习笔记(二)
- Java——方法重载
- codeforces上一种应对大测试数据debug方法
- 剑指Offer系列-面试题9:斐波那契数列
- 浙江大学复试编程题之——Sharing
- Android 关于Selector的使用
- stm32f1xx HAL库下载介绍
- [leetcode]35. Search Insert Position(java)
- 【推荐】《Android应用安全设计及安全编码指导手册》更新到2016年9月1日版本
- 数据结构(三)栈结构模拟编译器判断括号是否匹配
- .DS_Store 文件是什么? / .DS_Store 文件是什么macOS
- POJ 1321 基础搜索 DFS 二
- leetcode 141. Linked List Cycle