-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
249 lines (206 loc) · 4.37 KB
/
functions.cpp
File metadata and controls
249 lines (206 loc) · 4.37 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
class DisJointSet {
public:
vector<int> rank,p;
DisJointSet(){}
DisJointSet(int size){
rank.resize(size,0);
p.resize(size,0);
for(int i = 0; i < size; i++) makeSet(i);
}
void makeSet(int x){
p[x] = x;
rank[x] = 0;
}
bool same(int x, int y){
return findSet(x) == findSet(y);
}
void unite(int x, int y){
link(findSet(x), findSet(y));
}
void link(int x, int y){
if( rank[x] > rank[y] ){
p[y] = x;
}else {
p[x] = y;
if( rank[x] == rank[y]){
rank[y] ++;
}
}
}
int findSet(int x){
if ( x != p[x] ){
p[x] = findSet(p[x]);
}
return p[x];
}
};
//数値を分割して配列に入れることができる
void sliceDigit(int num, vector<int> &result){
while(num > 0){
result.push_back(num % 10);
num /= 10;
}
reverse(result.begin(),result.end());
}
//絶対値を求める
int absol(int a){
if( a > 0) return a;
return -a;
}
//10進数を2進数へ変換する
int binary(int bina){
int ans = 0;
for (int i = 0; bina>0 ; i++)
{
ans = ans+(bina%2)*pow(10,i);
bina = bina/2;
}
return ans;
}
//10進数を2進数のvectorへ
vector<int> dicimal_to_binary(int a){
vector<int> result;
while(a > 0){
result.pb(a % 2);
a = a / 2;
}
reverse(result.begin(),result.end());
return result;
}
//素数を判定する
bool isPrime(int x){
int i;
if ( x < 2 ) return false;
else if ( x == 2 ) return true;
if( x % 2 == 0 ) return false;
for( int i = 3; i*i <= x; i+=2 ){
if( x % i == 0) return false;
}
return true;
}
//最大公約数
int gcd(int x, int y){
int r;
if ( x < y ) swap(x,y);
while (y > 0){
r = x % y;
x = y;
y = r;
}
return x;
}
bool isAlphabet(char ch,int mode){
bool flag = false;
switch(mode){
case 0://小文字判定
if(ch - 'a' >= 0 && ch - 'z' <= 0){
flag = true;
}
break;
case 1://大文字
if(ch - 'A' >= 0 && ch - 'Z' <= 0){
flag = true;
}
break;
case 2://アルファベット全体
if(ch - 'A' >= 0 && ch - 'z' <= 0){
flag = true;
}
}
return flag;
}
//文字列を任意の文字で分割できる
//split("文字列","分解したい文字",入れる変数)
template <typename List>
void split(const std::string& s, const std::string& delim, List& result)
{
result.clear();
using string = std::string;
string::size_type pos = 0;
while(pos != string::npos )
{
string::size_type p = s.find(delim, pos);
if(p == string::npos)
{
result.push_back(s.substr(pos));
break;
}
else {
result.push_back(s.substr(pos, p - pos));
}
pos = p + delim.size();
}
}
int max (vector<int> array){
int maxval = 0;
for (int i = 0; i < array.size(); i ++) {
if( array[i] > maxval){
maxval = array[i];
}
}
return maxval;
}
static const int MAX = 10000;
static const int INFTY = (1<<20);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n;
vector<pair<int,int> > adj[MAX];
void dijkstra(){
priority_queue<pair<int,int> > PQ;
int color[10000];
int d[10000];
for( int i = 0; i < n; i++){
d[i] = INFTY;
color[i] = WHITE;
}
d[0] = 0;
PQ.push(make_pair(0,0));
color[0] = GRAY;
while( !PQ.empty() ){
pair<int,int> f = PQ.top(); PQ.pop();
int u = f.second;
color[u] = BLACK;
if( d[u] < f.first * (-1) ) continue;
for ( int j = 0; j < adj[u].size(); j++ ){
int v = adj[u][j].first;
if ( color[v] == BLACK ) continue;
if ( d[v] > d[u] + adj[u][j].second ){
d[v] = d[u] + adj[u][j].second;
PQ.push(make_pair(d[v] * (-1), v));
color[v] = GRAY;
}
}
}
}
void fileReader(string filename,vector<string> &vec){
ifstream ifs(filename);
string str;
while(getline(ifs,str)){
cout << "push" << " " <<str <<endl;
vec.push_back(str);
}
}
int strCount(string str,string aim){
int cnt = 0;
for (int i = 0; i < (int)str.length(); i++){
if (str[i] == aim[0]){
cnt += 1;
}
}
return cnt;
}
//sstreamのインクルードが必要
int string_to_int(std::string str){
int ret;
std::stringstream ss;
ss << str;
ss >> ret;
return ret;
}