PAT甲級1032 Sharing
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
whereAddress 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 9
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67890
00001 o 00010
Sample Output 1:
67890
Sample Input 2:
00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1
Sample Output 2:
-1
思路
思路就是將兩個單詞存入兩個矩陣,找公共尾綴。但是這題有點迷,題目上suffix應該是尾綴的意思,應該要兩個單詞尾部有公共鏈才算,但是我從尾向前遍歷找公共鏈會出錯,從頭到尾找第一個公共點反而對了…
#include <iostream>
#include <vector>
#include <map>
#include <cstdlib>
using namespace std;
struct node
{
int address;
char data;
int next;
};
int main()
{
int word1, word2, n;
cin >> word1 >> word2 >> n;
map<int, node> mp;
for (int i = 0; i < n; i++)
{
node s;
cin >> s.address >> s.data >> s.next;
mp[s.address] = s;
}
vector<node> w1, w2;
while (word1 != -1)
{
w1.push_back(mp[word1]);
word1 = mp[word1].next;
}
while (word2 != -1)
{
w2.push_back(mp[word2]);
word2 = mp[word2].next;
}
map<int, bool> check;
for (int i = 0; i < w1.size(); i++)
check[w1[i].address] = true;
int temp = -1;
for (int i = 0; i < w2.size(); i++)
{
if (check[w2[i].address] == true)
{
temp = w2[i].address;
break;
}
}
if (temp == -1)
cout << -1;
else
printf("%05d", temp);
system("pause");
return 0;
}
相關文章
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- PAT甲級1023 Have Fun with Number
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- PAT甲級1126~1130|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- 2024 秋季PAT認證甲級(題解A1-A4)
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- PAT-B 1032 挖掘機技術哪家強
- 2021.9.12週六PAT甲級考試覆盤與總結
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- 2020年7月第2題 PAT甲級真題 The Judger (25分)
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- PAT(甲級)2020年秋季考試 7-1 Panda and PP Milk (20分)
- PAT 乙級
- PTA甲級——Be Unique
- 2019年9月8日秋季PAT甲級題解-2-1161-Merging Linked Lists (25 分)
- PAT乙級1023
- 1021 Deepest Root(甲級)
- 【PAT乙級】1027 列印沙漏
- 【PAT乙級】1017 A除以B
- 【PAT乙級】1065 單身狗
- 【PAT乙級】1052 賣個萌
- 【PAT乙級】1048 數字加密加密
- 【PAT乙級】1066 影像過濾
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- [Vue] Sharing StateVue