浙江大学PAT_甲级_1032. Sharing (25)
来源:互联网 发布:抽奖软件 破解版 编辑:程序博客网 时间:2024/06/06 02:09
题目地址:点击打开链接
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).
Input Specification:
Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (<= 105), 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 Next
where 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.
Output Specification:
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.
Sample Input 1:11111 22222 967890 i 0000200010 a 1234500003 g -112345 D 6789000002 n 0000322222 B 2345611111 L 0000123456 e 6789000001 o 00010Sample Output 1:
67890Sample Input 2:
00001 00002 400001 a 1000110001 s -100002 a 1000210002 t -1Sample Output 2:
-1题意:输入两个单词的各位字符和地址和下一位字符地址,输出公共的字符部分的第一个字符的地址。
解法:把第一个单词的每一个字符放进集合set ,把第二个单词里的字符在set里进行查找,找到了就输出这个字符的地址。
我的C++代码:
#include <iostream> #include<set>using namespace std;int main(){int n, pointer1, pointer2 ;//n结点总个数, pointer1第一个单词首结点地址int nodeaddress[100000]; //数组保存各结点地址set<int>word1charset; //单词1字符组成的集合int addr, next; //addr地址,next下一个结点地址char ch; //结点字符信息cin >> pointer1 >> pointer2 >> n;for (int i = 0; i<n; i++){cin >> addr >> ch >> next;nodeaddress[addr] = next;//保存下一个结点的地址}while (pointer1 != -1)//当单词1字符没被遍历完{word1charset.insert(pointer1);//把单词1的字符添加进setpointer1 = nodeaddress[pointer1];//地址向后移}while (pointer2 != -1)//当单词2字符没被遍历完{if (word1charset.count(pointer2) != 0)//找到了相同的元素{printf("%05d", pointer2);//输出相同的字符的地址return 0;//结束退出程序}pointer2 = nodeaddress[pointer2];//地址向后移}printf("-1");return 0;}
- 浙江大学PAT_甲级_1032. Sharing (25)
- 浙江大学PAT_甲级_1024. Palindromic Number (25)
- 浙江大学PAT_甲级_1028. List Sorting (25)
- 浙江大学PAT_甲级_1029. Median (25)
- 浙江大学PAT_甲级_1048. Find Coins (25)
- 浙江大学PAT_甲级_1059. Prime Factors (25)
- 浙江大学PAT_甲级_1071. Speech Patterns (25)
- 浙江大学PAT_甲级_1083. List Grades (25)
- 浙江大学PAT_甲级_1070. Mooncake (25)
- 浙江大学PAT_甲级_1063. Set Similarity (25)
- 浙江大学PAT_乙级_1032. 挖掘机技术哪家强(20)
- 浙江大学PAT_甲级_1006. Sign In and Sign Out (25)
- 浙江大学PAT_甲级_1036. Boys vs Girls (25)
- 浙江大学PAT_甲级_1055. The World's Richest (25)
- 浙江大学PAT_甲级_1062. Talent and Virtue (25)
- 浙江大学PAT_甲级_1052. Linked List Sorting (25)
- 浙江大学PAT_甲级_1047. Student List for Course (25)
- 浙江大学PAT_甲级_1094. The Largest Generation (25)
- 枚举
- LeetCode ZigZag Conversion
- mysql 索引 建立 查询 优化
- Objective-C语法
- Oracle 不同环境下导入解决方案
- 浙江大学PAT_甲级_1032. Sharing (25)
- Service服务
- 带你玩转Visual Studio——带你理解多字节编码与Unicode码
- 由AVPlayer引发出来KVC的坑(KVC使用一定得注意)
- Javascript模块化编程(二):AMD规范
- oracle数据库管理员密码忘记怎么办
- Android 代码混淆和破解apk
- 概率问题
- ORM、JPA、Hibernate简单说明