problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
pat::b::b1105 命名空间参考

1105 链表合并 更多...

struct  node
 

函数

int main (istream &cin, ostream &cout)
 
 TEST (b1105, case1)
 

详细描述

1105 链表合并

函数说明

◆ main()

int pat::b::b1105::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp3743 行定义.

3743 {
3744 string h1;
3745 string h2;
3746 int n;
3747 cin >> h1 >> h2 >> n;
3748 unordered_map<string, node> um;
3749 for(int i = 0; i < n; i++) {
3750 string address;
3751 cin >> address;
3752 um[address].address = address;
3753 cin >> um[address].data >> um[address].next;
3754 }
3755 vector<node> l1;
3756 vector<node> l2;
3757 for(string addr = h1; addr != "-1"; addr = um[addr].next) {
3758 l1.push_back(um[addr]);
3759 }
3760 for(string addr = h2; addr != "-1"; addr = um[addr].next) {
3761 l2.push_back(um[addr]);
3762 }
3763 if(l1.size() < l2.size()) {
3764 swap(l1, l2);
3765 }
3766 l2 = vector(l2.rbegin(), l2.rend());
3767 vector<node> ans;
3768 for(int cnt = 0, i1 = 0, i2 = 0; i1 < l1.size() || i2 < l2.size(); cnt++, cnt %= 3) {
3769 if(cnt <= 1) {
3770 if(i1 < l1.size()) {
3771 ans.push_back(l1[i1++]);
3772 }
3773 } else {
3774 if(i2 < l2.size()) {
3775 ans.push_back(l2[i2++]);
3776 }
3777 }
3778 }
3779 for(int i = 0; i < ans.size(); i++) {
3780 cout << ans[i].address << ' ' << ans[i].data << ' ' << (i + 1 < ans.size() ? ans[i + 1].address : "-1") << endl;
3781 }
3782 return 0;
3783 }

被这些函数引用 TEST().

◆ TEST()

pat::b::b1105::TEST ( b1105  ,
case1   
)

在文件 pat_test.cpp1856 行定义.

1856 {
1857 istringstream in("00100 01000 7\n"
1858 "02233 2 34891\n"
1859 "00100 6 00001\n"
1860 "34891 3 10086\n"
1861 "01000 1 02233\n"
1862 "00033 5 -1\n"
1863 "10086 4 00033\n"
1864 "00001 7 -1");
1865 auto out = ostringstream();
1866 main(in, out);
1867 const auto ans = out.str();
1868 ASSERT_EQ("01000 1 02233\n"
1869 "02233 2 00001\n"
1870 "00001 7 34891\n"
1871 "34891 3 10086\n"
1872 "10086 4 00100\n"
1873 "00100 6 00033\n"
1874 "00033 5 -1\n",
1875 out.str());
1876 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().