日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PAT 1003 Sharing (25)

發布時間:2025/3/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT 1003 Sharing (25) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


題目描寫敘述

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).

輸入描寫敘述:

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.

輸出描寫敘述:

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.

輸入樣例:

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

輸出樣例:

67890



//最初的解法低效。且有未知錯誤 /*思路例如以下: 從最后面開始找。直到存在某個前驅有不止一個后繼指向它,則結束*/ #include <iostream> #include <iomanip>using namespace std;typedef struct Node {int addressPre,addressPost;char data; };int main() {int a1,a2,N,i,j,k,ans;while(cin>>a1>>a2>>N){Node *node=new Node[N];for(i=0;i<N;i++)cin>>node[i].addressPre>>node[i].data>>node[i].addressPost; /*for(i=0;i<N;i++)cout<<node[i].addressPre<<" "<<node[i].data<<" "<<node[i].addressPost<<endl; */k=-1;while(1){ans=0;for(i=0;i<N;i++){if(k==node[i].addressPost){ans++;j=i;} }if(ans>1)break;elsek=node[j].addressPre;}cout <<setfill('0')<< setw(5)<<k<<endl;}return 0; }


正解


#include <iostream> #include <iomanip> #include <vector>using namespace std;typedef struct Node {int addressPre,addressPost;char data; };vector<Node> list1; vector<Node> list2;Node node[100010];int main() {int a1,a2,N,i,j,k;int l1,l2;Node tNode;while(cin>>a1>>a2>>N){for(i=0;i<N;i++){cin>>tNode.addressPre>>tNode.data>>tNode.addressPost;node[tNode.addressPre]=tNode;}l1=a1;l2=a2;while(l1!=-1){list1.push_back(node[l1]);l1=node[l1].addressPost;}while(l2!=-1){list2.push_back(node[l2]);l2=node[l2].addressPost;}for(i=0,k=0;i<list1.size();i++){for(j=0;j<list2.size();j++){if(list1[i].addressPre==list2[j].addressPre){k=1;break;}}if(k)break;}if(k)cout <<setfill('0')<< setw(5)<<list1[i].addressPre<<endl;elsecout<<-1<<endl;}return 0; }



轉載于:https://www.cnblogs.com/clnchanpin/p/7202242.html

總結

以上是生活随笔為你收集整理的PAT 1003 Sharing (25)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。