-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.cpp
67 lines (61 loc) · 1.49 KB
/
common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include "common.h"
#include <algorithm>
//This file is for common functions
//function: convert long to string
string itos (long a)
{
ostringstream str;
str<<a;
return str.str();
}
//function: split Str
void SplitStr(vector<string> & splits, const string& str, const string & deli)
{ // Note: splits are never cleared inside!!!
size_t start, end;
start = str.find_first_not_of(deli);
while (start != string::npos)
{
end = str.find_first_of(deli, start + 1);
if (end != string::npos)
{
splits.push_back(str.substr(start, end - start));
start = str.find_first_not_of(deli, end + 1);
}
else
{
splits.push_back(str.substr(start));
break;
}
}
}
// find common vertices between two sorted subsets
vector<long> findCommonV(vector<long> &S1, vector<long> &S2)
{
/* Must sort 2 subsets before call this function */
sort(S1.begin(),S1.end());
sort(S2.begin(),S2.end());
vector<long> commonV;
if (S1.empty() || S2.empty()) {
return commonV;
}
long pos1=0;
long pos2 =0;
long p1,p2;
long end1 = S1.size(), end2 = S2.size();
while(pos1<end1 && pos2<end2)
{
p1 = S1[pos1];
p2 = S2[pos2];
if (p1 == p2) {
commonV.push_back(p1);
pos1++;
pos2++;
}
else if (p1>p2)
pos2++;
else
pos1++;
}
return commonV;
}