text
stringlengths
2
104M
meta
dict
/* The MIT License (MIT) Copyright (c) 2014 Mustafa Ahmed Abduljabbar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #import "WirelessAPI.h" @implementation WirelessAPI static WirelessAPI *sharedApi= nil; +(id)getSharedInstance{ if (sharedApi == nil) { sharedApi = [[super allocWithZone:NULL]init]; } return sharedApi; } -(id)init{ if (self = [super init]) { //// This Required you to put your application under /Application/ in order to access this private library network = [[NSMutableDictionary alloc]init]; networks = [[NSMutableArray alloc]init]; libHandle = dlopen("/System/Library/SystemConfiguration/IPConfiguration.bundle/IPConfiguration", 0x1); char *error; if (libHandle == NULL && (error = dlerror()) != NULL) { NSLog(@"%s",error); exit(1); } apple80211Open = dlsym(libHandle, "Apple80211Open"); apple80211Bind = dlsym(libHandle, "Apple80211BindToInterface"); apple80211Close = dlsym(libHandle, "Apple80211Close"); apple80211Scan = dlsym(libHandle, "Apple80211Scan"); apple80211Open(&airportHandle); apple80211Bind(airportHandle, @"en0"); //// This is the wireless interface . if you are a little bit hacky you can plugin your own wireless device and write it's own interface ! } return self; } -(NSArray *)fatchWifiSignles{ NSArray *scan_networks; apple80211Scan(airportHandle, &scan_networks, NULL); //// You can replace NULL With some parameters ... [networks setArray:scan_networks]; return networks; } -(NSDictionary *)networkInfo:(NSString *)bssid{ [self fatchWifiSignles]; for (int i = 0; i < [networks count]; i++) { [network setObject:[networks objectAtIndex: i] forKey:[[networks objectAtIndex: i] objectForKey:@"BSSID"]]; } return [network objectForKey:bssid]; } -(void)turnDown{ apple80211Close(airportHandle); } @end
{ "repo_name": "mustafa96m/iOS-Wireless-Scanner-API", "stars": "30", "repo_language": "Objective-C", "file_name": "README.md", "mime_type": "text/plain" }
iOS-Wireless-Scanner-API ======================== ## Notices * Only works on jailbreaked iOS Devices * Can't be published in AppStore due to the usage of Private Libraries * Works perfectly on iOS 7.0+ ( Not tested on previous versions ) ##Usage The API built on Signleton Pattern so you only need to import *Wireless.h* into your project To get all wireless signels in *NSArray* ```Objective-C [[WirelessAPI getSharedInstance]fatchWifiSignles]] ``` Or to get specific signel as *NSDictionary* ```Objective-C [[WirelessAPI getSharedInstance]networkInfo:@"11:22:33:44:55"]; /// Replace it with signel bssid ``` **Important !!** you need to put turnDown method under *applicationWillTerminate* in AppDelgate.m ```Objective-C - (void)applicationWillTerminate:(UIApplication *)application { [[WirelessAPI getSharedInstance]turnDown]; } ``` ## Sample of returned information ( JSONed Version ) ```JSON { "0:19:70:86:83:65": { "AGE": 23752, "AP_MODE": 2, "BEACON_INT": 20, "BSSID": "0:19:70:86:83:65", "CAPABILITIES": 1057, "CHANNEL": 11, "CHANNEL_FLAGS": 8, "HT_CAPS_IE": { "AMPDU_PARAMS": 27, "ASEL_CAPS": 20780, "EXT_CAPS": 0, "MCS_SET": "ff000000 00000000 00000000 00000000", "TXBF_CAPS": 0 }, "HT_IE": { "HT_BASIC_MCS_SET": "00000000 00000000 00000000 00000000", "HT_DUAL_BEACON": 0, "HT_DUAL_CTS_PROT": 0, "HT_LSIG_TXOP_PROT_FULL": 0, "HT_NON_GF_STAS_PRESENT": 0, "HT_OBSS_NON_HT_STAS_PRESENT": 1, "HT_OP_MODE": 1, "HT_PCO_ACTIVE": 0, "HT_PCO_PHASE": 0, "HT_PRIMARY_CHAN": 11, "HT_PSMP_STAS_ONLY": 0, "HT_RIFS_MODE": 0, "HT_SECONDARY_BEACON": 0, "HT_SECONDARY_CHAN_OFFSET": 0, "HT_SERVICE_INT": 0, "HT_STA_CHAN_WIDTH": 0, "HT_TX_BURST_LIMIT": 1 }, "IE": "2d1a2c51 1bff0000 00000000 00000000 00000000 00000000 00000000 3d160b00 19000000 00000000 00000000 00000000 00000000 7f01017f 0101", "NOISE": "-92", "PHY_MODE": 16, "RATES": [ 1, 2, 5, 11, 6, 9, 12, 18, 24, 36, 48, 54 ], "RSSI": "-98", "SCAN_RESULT_FROM_PROBE_RSP": 0, "SNR": 3, "SSID": "4d722e43 6f6d7075 3213123 5f303739 1123322 3333111", "SSID_STR": "Test SSID" } } ``` ## Build you need to install the applictaion in main /Application directory in order to access private frameworks so in order to build you need to do first Command + Shift + I then go to Products -> Show In folder -> Back to previous dir you must find Release-YourApplicationName folder upload XXXXX.app to your device by scp to /Application and reboot your device ## To-Do List - [ ] Filtering API (Working On) - [ ] Fully Functional App ## Thanks Your Ideas , Collaborations , Code Review are highly welcomed and appreciated and again thanks for being awesome :) any question @mustafa96m on twitter you need to checkout for more detailed information :) https://code.google.com/p/iphone-wireless/ https://code.google.com/p/iphone-wireless/wiki/Stumbler ( Awesome ) ## License The MIT License (MIT) Copyright (c) 2014 Mustafa Ahmed Abduljabbar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{ "repo_name": "mustafa96m/iOS-Wireless-Scanner-API", "stars": "30", "repo_language": "Objective-C", "file_name": "README.md", "mime_type": "text/plain" }
#include <cstdio> #define LOWBIT(x) ((x) & (-x)) #define ELEMENT_COUNT 1050 #define MOD 45679 using namespace std; // Array D must begin with 1. int D[ELEMENT_COUNT], T[ELEMENT_COUNT]; // The size of D; int n = 1024; void build_tree(int c) { for (int i = 1; i <= c; i++) { for (int j = i - LOWBIT(i) + 1; j <= i; j++) { T[i] += D[j]; } } } // Return the sum of 1..k. int get_sum(int k) { int res = 0; while (k > 0) { res += T[k]; k -= LOWBIT(k); } return res; } // D[a] += x and update the tree. void add_one(int a, int x) { while (a <= n) { T[a] = (T[a] + x) % MOD; a += LOWBIT(a); } } int main() { // e.g. CodeVS - 3900. for (int i = 1; i <= 1024; i++) { D[i] = 1; } build_tree(1024); int m; scanf("%d", &m); for (int i = 0; i < m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); if (a == 1) { int pre = D[b]; D[b] = D[b] * c % MOD; add_one(b, D[b] - pre + MOD); } else { printf("%d\n", ((get_sum(c) - get_sum(b - 1)) % MOD + MOD) % MOD); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> using namespace std; int _global_length_; class lint { private: int s_length, n_length, *num; char *temp; int max(int x, int y); void clear(); int nextshort(int offset); lint mul1(int x, int offset); public: lint(int len = _global_length_); void read(); void write(); lint operator + (lint x); lint operator - (lint x); lint operator * (lint x); bool operator < (lint x); }; int lint::max(int x, int y) { return x > y ? x : y; } void lint::clear() { memset(num, 0, n_length * sizeof(num[0])); } int lint::nextshort(int offset) { int res = 0; for (int i = max(offset - 3, 0); i <= offset; i++) { res = res * 10 + (temp[i] - '0'); } return res; } lint lint::mul1(int x, int offset) { lint res(s_length); int d = 0; for (int i = 0; i + offset < n_length; i++) { d = num[i] * x + d; res.num[i + offset] = d % 10000; d /= 10000; } return res; } lint::lint(int len) { s_length = len; n_length = len / 4; temp = new char[s_length]; num = new int[n_length]; clear(); } void lint::read() { scanf("%s", temp); clear(); int l = strlen(temp), k = 0; for (int i = l - 1; i >= 0; i -= 4) { num[k++] = nextshort(i); } } void lint::write() { int i; for (i = n_length - 1; i >= 0; i--) { if (num[i] != 0) { break; } } if (i == -1) { printf("0"); return; } printf("%d", num[i]); for (i--; i >= 0; i--) { printf("%04d", num[i]); } } lint lint::operator + (lint x) { lint res(s_length); int d = 0; for (int i = 0; i < n_length; i++) { d += num[i] + x.num[i]; res.num[i] = d % 10000; d /= 10000; } return res; } lint lint::operator - (lint x) { lint res(s_length); int d = 0; for (int i = 0; i < n_length; i++) { d += num[i] - x.num[i]; res.num[i] = (d + 10000) % 10000; d = d < 0 ? -1 : 0; } return res; } lint lint::operator * (lint x) { lint res(s_length), t(s_length); for (int i = 0; i < s_length; i++) { t = mul1(x.num[i], i); res = res + t; } return res; } bool lint::operator < (lint x) { for (int i = n_length - 1; i >= 0; i--) { if (num[i] < x.num[i]) { return true; } else if (num[i] > x.num[i]) { return false; } } return false; } int main() { lint a(100), b(100); char op[10]; while (true) { a.read(); scanf("%s", op); b.read(); switch (op[0]) { case '+': (a + b).write(); break; case '-': if (a < b) { printf("-"); (b - a).write(); } else { (a - b).write(); } break; case '*': (a * b).write(); break; default: printf("Invalid operator."); break; } printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct node { node *lch, *rch; int key, dis; node(int _key = 0, int _dis = 0); }*root, *nil; node::node(int _key, int _dis) : key(_key), dis(_dis) { lch = rch = nil; } void init() { nil = new node(0, -1); root = nil; } inline void swap(node *&u, node *&v) { node *temp = u; u = v; v = temp; } void merge(node *&u, node *&v) { if (u == nil) { u = v; return; } if (v == nil) return; if (u->key > v->key) swap(u, v); merge(u->rch, v); if (u->lch->dis < u->rch->dis) swap(u->lch, u->rch); u->dis = u->rch->dis + 1; } int extract_min() { int res = root->key; merge(root->lch, root->rch); node *del = root; root = root->lch; delete del; return res; } int main() { int op, x; init(); while (true) { scanf("%d", &op); if (op == 1) { scanf("%d", &x); node *o = new node(x, 0); merge(root, o); } else if (op == 2) { printf("%d\n", extract_min()); } else if (op == 0) { break; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MOD 1000000000 #define MAX 1000 int C[MAX][MAX]; void init_C() { for (int i = 0; i < MAX; i++) { C[i][0] = 1; } for (int i = 1; i < MAX; i++) { for (int j = 1; j <= i; j++) { C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; } } } int main() { int n, m; init_C(); while (scanf("%d%d", &n, &m) > 0) { printf("%d\n", C[n][m]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 using namespace std; struct node { int id, value; node *next; }; node *head; void insert(int id, int value) { node *o = new node; o->next = head; head = o; o->id = id; o->value = value; } int find(int id) { node *cur = head; while (cur != NIL) { if (cur->id == id) { return cur->value; } cur = cur->next; } return -1; } bool del(int id) { if (head == NIL) { return false; } if (head->id == id) { node *temp = head; head = head->next; delete temp; return true; } node *prev = head, *cur = head->next; while (cur != NIL) { if (cur->id == id) { prev->next = cur->next; delete cur; return true; } prev = cur; cur = cur->next; } return false; } int main() { int in, id, value; while (true) { scanf("%d", &in); if (in == 1) { scanf("%d%d", &id, &value); insert(id, value); } else if (in == 2) { scanf("%d", &id); printf("%d\n", find(id)); } else if (in == 3) { scanf("%d", &id); printf("%s\n", del(id) == true ? "deleted" : "failed"); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 using namespace std; struct vertex { int first, ideg, odeg; bool vis; }V[100010]; struct edge { int op, endp, next; }E[200010]; int ec = 1, ivc, iec, cnt = 0; int path[200010], pc; void add_edge(int u, int v) { V[u].odeg++; V[v].ideg++; E[ec].next = V[u].first; V[u].first = ec; E[ec].op = u; E[ec].endp = v; ec++; } void del_edge(int ord) { V[E[ord].op].first = E[ord].next; } void connect(int u) { if (V[u].vis == false) { cnt++; V[u].vis = true; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { connect(E[cur].endp); } } } void DFS(int u, int start) { int first = V[u].first; del_edge(first); if (E[first].endp != start) { DFS(E[first].endp, start); } path[pc++] = first; while (V[u].first != NIL) { DFS(u, u); } } int main() { int k, u, v; scanf("%d%d%d", &k, &ivc, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d", &u, &v); add_edge(u, v); } for (int i = 1; i <= ivc; i++) { if (V[i].ideg != V[i].odeg) { printf("NO"); return 0; } if (V[i].ideg == 0) { cnt++; } } int vhe; for (int i = 1; i <= ivc; i++) { if (V[i].ideg != 0) { connect(i); vhe = i; break; } } if (cnt < ivc) { printf("NO"); return 0; } printf("YES\n"); if (iec == 0) { return 0; } DFS(vhe, vhe); for (int i = pc - 1; i >= 0; i--) { printf("%d ", path[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #include <ctime> #include <utility> using namespace std; struct node { node *lch, *rch; int key, pr; node(int _key); }*root, *nil; node::node(int _key = 0) : lch(nil), rch(nil), key(_key), pr(rand()) { } void init() { nil = new node; root = nil; } pair<node*, node*> split(node *u, int x) { if (u == nil) { return make_pair(nil, nil); } if (x <= u->key) { pair<node*, node*> res = split(u->lch, x); u->lch = res.second; return make_pair(res.first, u); } else { pair<node*, node*> res = split(u->rch, x); u->rch = res.first; return make_pair(u, res.second); } } node* merge(node *u, node *v) { if (u == nil) { return v; } else if (v == nil) { return u; } if (u->pr > v->pr) { u->rch = merge(u->rch, v); return u; } else { v->lch = merge(u, v->lch); return v; } } int find(node *u, int x) { if (u == nil) { return 0; } if (u->key == x) { return 1; } if (x < u->key) { return find(u->lch, x); } else { return find(u->rch, x); } } int find(int x) { return find(root, x); } void insert(int x) { if (find(root, x) == 1) { return; } node *u = new node(x); pair<node*, node*> rts = split(root, x); rts.first = merge(rts.first, u); root = merge(rts.first, rts.second); } void print_tree(node *u) { if (u != nil) { printf("("); print_tree(u->lch); printf("%d", u->key); print_tree(u->rch); printf(")"); } } int main() { char op[10]; int x; init(); while (true) { scanf("%s", op); if (strcmp(op, "insert") == 0) { scanf("%d", &x); insert(x); } else if (strcmp(op, "find") == 0) { scanf("%d", &x); printf("%d\n", find(x)); } else if (strcmp(op, "exit") == 0) { break; } else { printf("Command not found.\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 using namespace std; struct vertex { int first_edge; int dis; }V[100010]; struct edge { int endp, next; int w; }E[1000010]; int ec = 1; int ivc, iec; int Q[200000]; int hp = 0, tp = 0; void enqueue(int x) { Q[tp] = x; tp++; tp %= 200000; } int dequeue() { int ret = Q[hp]; hp++; hp %= 200000; return ret; } bool empty() { return hp == tp; } void add_edge(int u, int v, int w) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; E[ec].w = w; ec++; } void initial_single_source(int s) { for (int i = 1; i <= ivc; i++) { V[i].dis = INF; } V[s].dis = 0; } void SPFA(int s) { initial_single_source(s); enqueue(s); while (!empty()) { int u = dequeue(); for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { int newdis = V[u].dis + E[cur].w; if (newdis < V[E[cur].endp].dis) { V[E[cur].endp].dis = newdis; enqueue(E[cur].endp); } } } } int main() { scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w); add_edge(v, u, w); } SPFA(1); printf("%d", V[ivc].dis); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define ELEMENT_COUNT 10000 using namespace std; int n, k, d[ELEMENT_COUNT]; int partition(int l, int r) { int x = d[r], j = l - 1; for (int i = l; i <= r; i++) { if (d[i] <= x) { j++; int temp = d[i]; d[i] = d[j]; d[j] = temp; } } return j + 1; } int select(int k) { int l = 0, r = n - 1; while (l < r) { int ord = partition(l, r); if (ord < k) { l = ord; } else if (ord > k) { r = ord - 2; } else { return d[ord - 1]; } } return d[l]; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } int kth = select(k); printf("%d", kth); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> typedef long long ll; using namespace std; ll p; ll qpow(ll x, ll y) { if (y == 1) return x; ll t = qpow(x, y >> 1); t = t * t % p; if ((y & 1) == 0) return t; return t * x % p; } int main() { ll x; while (true) { scanf("%lld", &p); for (ll k = 2; ; k++) { x = p - 1; for (ll i = 2; i * i <= x; i++) { if (x % i == 0) { if (qpow(k, (p - 1) / i) == 1) goto NEXT; while (x % i == 0) x /= i; } } if (x != 1) { if (qpow(k, (p - 1) / x) == 1) goto NEXT; } printf("%lld\n", k); break; NEXT:; } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #include <queue> #define INF 2000000000 #define VERTEX_COUNT 100000 #define EDGE_COUNT 100000 using namespace std; struct vertex { int first, dis; }V[VERTEX_COUNT]; struct edge { int endp, next, f; }E[EDGE_COUNT]; int ivc, iec, ec = 2, src, sink; inline void add_edge(int u, int v, int f) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; E[ec].f = f; ec++; } bool bfs() { queue<int> Q; static bool inq[VERTEX_COUNT]; memset(inq, false, sizeof(inq)); Q.push(sink), inq[sink] = true; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (E[cur ^ 1].f > 0 && inq[E[cur].endp] == false) { V[E[cur].endp].dis = V[u].dis + 1; Q.push(E[cur].endp), inq[E[cur].endp] = true; } } } return inq[src] == true; } int dfs(int u, int curf) { if (u == sink) return curf; int totalf = 0; for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next) { if (V[u].dis == V[E[cur].endp].dis + 1 && E[cur].f > 0) { int f = dfs(E[cur].endp, min(E[cur].f, curf - totalf)); E[cur].f -= f; E[cur ^ 1].f += f; totalf += f; } } return totalf; } int max_flow() { int res = 0; while (bfs() == true) { int flow; do { flow = dfs(src, INF); res += flow; } while (flow > 0); } return res; } int main() { int u, v, f; scanf("%d%d", &iec, &ivc); for (int i = 0; i < iec; i++) { scanf("%d%d%d", &u, &v, &f); add_edge(u, v, f); add_edge(v, u, 0); } src = 1, sink = ivc; printf("%d", max_flow()); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct point { int x, y; }; struct segment { point begin, end; }; int direction(segment &base, segment &s) { return (s.end.x - s.begin.x) * (base.end.y - base.begin.y) - (base.end.x - base.begin.x) * (s.end.y - s.begin.y); } int main() { while (true) { segment base, s; scanf("%d%d%d%d%d%d%d%d", &base.begin.x, &base.begin.y, &base.end.x, &base.end.y, &s.begin.x, &s.begin.y, &s.end.x, &s.end.y); printf("%d\n", direction(base, s)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <algorithm> #include <vector> using namespace std; struct vertex { int first_edge, dfn, low, col; bool instack; }V[100010]; struct edge { int endp, next; }E[500010]; struct res { vector<int> block; }*R; int ec = 1, ivc, iec, dfn = 1, col = 1; int S[100000], sp = 0; void push(int x) { V[x].instack = true; S[sp++] = x; } int pop() { V[S[sp - 1]].instack = false; return S[--sp]; } void add_edge(int u, int v) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; ec++; } void DFS(int u) { V[u].dfn = V[u].low = dfn++; push(u); for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].dfn == 0) { DFS(E[cur].endp); } if (V[E[cur].endp].instack == true && V[E[cur].endp].low < V[u].low) { V[u].low = V[E[cur].endp].low; } } if (V[u].low == V[u].dfn) { int x = pop(); while (x != u) { V[x].col = col; x = pop(); } V[x].col = col; col++; } } bool _res_cmp(res x, res y) { return x.block[0] < y.block[0]; } int main() { int u, v; scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d", &u, &v); add_edge(u, v); } for (int i = 1; i <= ivc; i++) { if (V[i].dfn == 0) { DFS(i); } } R = new res[col + 1]; for (int i = 1; i <= ivc; i++) { R[V[i].col].block.push_back(i); } sort(R + 1, R + col, _res_cmp); for (int i = 1; i < col; i++) { printf("%d", R[i].block[0]); for (int j = 1; j < R[i].block.size(); j++) { printf(" %d", R[i].block[j]); } printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } int main() { int a, b; while (true) { scanf("%d%d", &a, &b); printf("%d\n", gcd(a, b)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define NIL 0 #define LAYER_COUNT 20 #define NODE_COUNT 100000 using namespace std; struct node { int id, value; node *next, *lower; node(); }; node::node() : next(NIL) { } node *head[LAYER_COUNT]; void init() { head[0] = new node; for (int i = 1; i < LAYER_COUNT; i++) { head[i] = new node; head[i]->lower = head[i - 1]; } } void insert(int id, int value) { int k = 0; while ((rand() & 1) == 0) { k++; } node *prev = head[k], *cur = head[k], *upper = NIL; for (; k >= 0; k--) { while (cur != NIL && cur->id < id) { prev = cur; cur = cur->next; } node *o = new node; o->next = cur; o->id = id; o->value = value; prev->next = o; prev = prev->lower; cur = prev; if (upper != NIL) { upper->lower = o; } upper = o; } } int find(int id) { node *prev, *cur = head[LAYER_COUNT - 1]; for (int k = LAYER_COUNT - 1; k >= 0; k--) { while (cur != NIL && cur->id < id) { prev = cur; cur = cur->next; } if (cur != NIL && cur->id == id) { return cur->value; } else { prev = prev->lower; cur = prev; } } return -1; } void debug() { for (int i = 5; i >= 0; i--) { node *cur = head[i]; printf("head[%d]", i); while (cur != NIL) { printf(" -> (%d, %d)", cur->id, cur->value); cur = cur->next; } printf("\n"); } } int main() { int in, id, value; init(); while (true) { scanf("%d", &in); if (in == 1) { scanf("%d%d", &id, &value); insert(id, value); } else if (in == 2) { scanf("%d", &id); printf("%d\n", find(id)); } else if (in == 4) { debug(); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MOD 1000000007 #define MAX_ELEMENT 10000000 int rev[MAX_ELEMENT]; void sieve(int n) { rev[1] = 1; for (int i = 2; i <= n; i++) { rev[i] = (int)(-(long long)(MOD / i) * (long long)rev[MOD % i] % (long long)MOD); rev[i] = (rev[i] % MOD + MOD) % MOD; } } int main() { int n; scanf("%d", &n); sieve(n); for (int i = 1; i <= n; i++) { printf("rev(%d) = %d\n", i, rev[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define VERTEX_COUNT 100000 #define EDGE_COUNT 100000 using namespace std; struct vertex { int first, dfn, low; }V[VERTEX_COUNT]; struct edge { int endp, next, col; }E[EDGE_COUNT]; int ec = 2, ivc, iec, S[VERTEX_COUNT], sp = 0, dfn = 1, col = 1; inline int min(int x, int y) { return x < y ? x : y; } inline void push(int e) { S[sp++] = e; } inline int pop() { return S[--sp]; } void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } void DFS(int u, int p) { V[u].dfn = V[u].low = dfn++; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].dfn == 0) { push(cur); DFS(E[cur].endp, u); V[u].low = min(V[u].low, V[E[cur].endp].low); if (V[E[cur].endp].low >= V[u].dfn) { int e; do { e = pop(); E[e].col = col; E[e ^ 1].col = col; } while (e != cur); col++; } } else if (E[cur].endp != p) { push(cur); V[u].low = min(V[u].low, V[E[cur].endp].dfn); } } } int main() { int u, v; scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } DFS(1, 0); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define MAX 100 using namespace std; bool isprime[MAX + 1]; int main() { memset(isprime, true, sizeof(isprime)); for (int i = 2; i * i <= MAX; i++) { if (isprime[i] == true) { for (int j = i * i; j <= MAX; j += i) { isprime[j] = false; } } } for (int i = 2; i < MAX; i++) { if (isprime[i] == true) { printf("%d\n", i); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 #define ELEMENT_COUNT 100000 using namespace std; int n, d[ELEMENT_COUNT], ans; void merge(int o1, int l1, int o2, int l2) { static int a1[ELEMENT_COUNT], a2[ELEMENT_COUNT]; int k1 = 0, k2 = 0; for (int i = 0; i < l1; i++) { a1[i] = d[i + o1]; } a1[l1] = INF; for (int i = 0; i < l2; i++) { a2[i] = d[i + o2]; } a2[l2] = INF; for (int i = 0; i < l1 + l2; i++) { if (a1[k1] <= a2[k2]) { d[i + o1] = a1[k1++]; } else { d[i + o1] = a2[k2++]; ans += (l1 - k1); } } } void merge_sort(int l, int r) { if (l < r) { int mid = (l + r) >> 1; merge_sort(l, mid); merge_sort(mid + 1, r); merge(l, mid - l + 1, mid + 1, r - mid); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } merge_sort(0, n - 1); printf("%d", ans); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 #define VERTEX_COUNT 210 #define EDGE_COUNT 410 using namespace std; struct vertex { int first, dis; }V[VERTEX_COUNT]; struct edge { int endp, next, lower, upper, flow; }E[EDGE_COUNT]; int ivc, iec, ec = 2, gap[VERTEX_COUNT], src, sink; inline int min(int x, int y) { return x < y ? x : y; } void init() { src = 1; sink = ivc; gap[0] = ivc; } void add_edge(int u, int v, int lower, int upper, int flow) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; E[ec].lower = lower; E[ec].upper = upper; E[ec].flow = flow; ec++; } int isap(int u, int curf) { if (u == sink) { return curf; } int totalf = 0, mindis = ivc; for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next) { if (E[cur].flow > 0) { if (V[u].dis == V[E[cur].endp].dis + 1) { int f = isap(E[cur].endp, min(E[cur].flow, curf - totalf)); totalf += f; E[cur].flow -= f; E[cur ^ 1].flow += f; } mindis = min(mindis, V[E[cur].endp].dis); } } if (totalf == 0) { gap[V[u].dis]--; if (gap[V[u].dis] == 0) V[src].dis = ivc; V[u].dis = mindis + 1; gap[V[u].dis]++; } return totalf; } int max_flow() { int res = 0; while (V[src].dis < ivc) { res += isap(src, INF); } return res; } int main() { int u, v, f; scanf("%d%d", &iec, &ivc); for (int i = 0; i < iec; i++) { scanf("%d%d%d", &u, &v, &f); add_edge(u, v, 0, 0, f); add_edge(v, u, 0, 0, 0); } init(); printf("%d", max_flow()); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
# Algorithms &emsp;&emsp;这里有各种算法的C++代码,任何人可以在自己的任何程序中使用。欢迎大家指出代码中的错误以及有待改进的地方。 &emsp;&emsp;本仓库内所有代码的授权方式为The Unlicense。大家如果使用我的代码开发自己的软件挣了大钱,或是参考我的代码在信息学奥林匹克竞赛中获得金牌,我都会很高兴的。使用这里的代码之后,你可以自主选择是否公开源代码。总而言之,你可以把这里的代码当作你自己写的一样,无论怎样使用都是被允许的。但是,我不对本仓库内代码的正确性负责。大家要是使用我的代码开发软件而导致程序崩溃,或是参考我的代码在考试时出错,请不要向我抱怨。如果你愿意,遇到问题可以在Issues中提出来,我们共同解决。 &emsp;&emsp;本仓库有两个分支,master和candidate。master用于存放原作者自己编写的代码,candidate接受所有合理的Pull Request。 &emsp;&emsp;以下索引提供了本仓库内算法的中文名,方便大家查找。此列表更新可能有较长时间的延迟,不保证所有已提交算法的名称都在列表中出现。 ## *Index* | --------------------------Contents-------------------------- | --------------------------FileName-------------------------- | | ------------------------------------------------------------ | ------------------------------------------------------------ | | 2-SAT可满足性 | 2-Satisfiability | | AC自动机 | Aho-Corasick-Automaton | | 单源最短路径(SPFA) | Bellman-Ford(Queue-Optimised) | | 单源最短路径(Bellman-Ford) | Bellman-Ford | | 双连通分量 | Biconnected-Compotent | | 使用Edmonds-Karp进行二分图匹配 | Bigrpah-Matching(Edmonds-Karp) | | 使用ISAP算法进行二分图匹配 | Bigraph-Matching(Improved-Shortest-Augmenting-Path) | | 普通的二叉搜索树 | Binary-Search-Tree | | 广度优先搜索 | Breadth-First-Search | | 冒泡排序 | Bubble-Sort | | 桶排序 | Bucket-Sort | | 笛卡尔树 | Cartesian-Tree | | 求解多边形的重心 | Centre-of-Gravity(Polygon) | | 组合数的递推求解 | Combination(Recursion) | | 枚举组合 | Combination | | 基本的复数类 | Complex-Number | | 割点 | Cut-Vertex | | 深度优先搜索 | Depth-First-Search | | 堆优化的Dijkstra算法 | Dijkstra(Heap-Optimised) | | Dinic最大流算法 | Dinic | | 并查集 | Disjoint-Set-Union | | 最大流Edmonds-Karp算法 | Edmonds-Karp | | 欧拉函数的线性筛法 | Euler's-Totient-Function(Linear) | | 欧拉函数 | Euler's-Totient-Function | | 有向图的欧拉回路 | Eulerian-Tour(Digraph) | | 拓展欧几里得算法 | Extended-Euclid | | 快速幂 | Fast-Exponentiation | | 快速数论变换(NTT) | Fast-Number-Theoretic-Transform | | 树状数组 | Fenwick-Tree | | 斐波那契堆 | Fibonacci-Heap | | 所有结点对之间的最短路径(Floyd) | Floyd-Warshall | | 高斯消元 | Gaussian-Elimination | | 凸包算法(Graham扫描法) | Graham-Scan | | 辗转相除法求最大公约数 | Greatest-Common-Divisor | | 堆排序 | Heap-Sort | | 树链剖分(轻重链剖分) | Heavy-Light-Decomposition | | 高精度类 | High-Precision(Integer) | | 匈牙利算法 | Hungarian-Algorithm | | 具有gap优化的ISAP算法 | Improved-Shortest-Augmenting-Path(Gap-Optimised) | | 朴素的ISAP算法 | Improved-Shortest-Augmenting-Path(Naive) | | 插入排序 | Insertion-Sort | | K-D树 | K-Dimensional-Tree | | KMP算法 | Knuth-Morris-Pratt | | Kruskal算法 | Kruskal | | 最近公共祖先(Tarjan) | Least-Common-Ancestor(Tarjan) | | 左偏树 | Leftist-Tree | | 线性基 | Linear-Basis | | LCT | Link-Cut-Tree | | LCT(带翻转) | Link-Cut-Tree(with-Reverse) | | 使用后缀数组求解最长公共子串 | Longest-Common-Substring | | 最长上升子序列(n·log(n)) | Longest-Increasing-Subsequence(n·log(n)) | | 倍增法求最近公共祖先 | Lowest-Common-Ancestor(Doubling) | | 朴素的矩阵乘法 | Matrix-Multiplication(Naive) | | 归并排序 | Merge-Sort | | Miller-Rabin素数测试 | Miller-Rabin | | 最小堆 | Min-Heap | | 阶乘的乘法逆元线性筛 | Modular-Multiplicative-Inverse(Factorial,Linear) | | 乘法逆元线性筛 | Modular-Multiplicative-Inverse(Linear) | | 乘法逆元 | Modular-Multiplicative-Inverse | | 无旋式Treap | Non-Rotating-Treap | | 回文树 | Palindromic-Tree | | 枚举排列 | Permutation | | 可持久化数组 | Persistent-Array | | 仅支持单点修改的可持久化线段树(维护区间和值) | Persistent-Segment-Tree(Sum) | | 可持久化Treap | Persistent-Treap | | 可持久化Trie | Persistent-Trie | | Prim算法 | Prim | | 试除法素数测试 | Prime-Check(Naive) | | 线性的素数筛法 | Prime-Sieve(Linear) | | 原根 | Primitive-Root | | Prüfer序列 | Prüfer-Sequence(Tree-to-Sequence) | | 队列的基本操作 | Queue | | 快速排序的优化版本 | Quick-Sort(Extra-Optimised) | | 快速排序的随机化版本 | Quick-Sort(Randomized) | | 快速排序 | Quick-Sort | | 基数排序 | Radix-Sort | | 使用归并排序求逆序对个数 | Reverse-Pair(Merge-Sort) | | 使用向量叉积判断两个有向线段的时针关系 | Segment-Direction | | 求两个线段的交点 | Segment-Intersection | | 线段树维护区间最小值 | Segment-Tree(Minimum) | | 线段树维护区间和值 | Segment-Tree(Sum) | | 选择排序 | Selection-Sort | | 普通的选择算法 | Selection | | 希尔排序 | Shell-Sort(Shell's-Gap-Sequence) | | Eratosthenes素数筛法 | Sieve-of-Erotosthenes | | 指针版的单向链表 | Singly-Linked-List(Pointer) | | 跳表 | Skip-List | | ST表 | Sparse-Table | | 记录父结点的伸展树 | Splay-with-Parent(Array) | | 伸展树 | Splay | | 单旋“伸展树” | Splay(Single-Rotation) | | 博弈论SG函数 | Sprague-Grundy | | 栈的基本操作 | Stack | | 递推法求解无符号第一类斯特林数 | Stirling-Number(Cycle,Unsigned,Recursion) | | 递推法求解第二类斯特林数 | Stirling-Number(Subset,Recursion) | | 倍增法求解后缀数组 | Suffix-Array(Doubling) | | 倍增法求解后缀数组(附带Height数组) | Suffix-Array-with-Height(Doubling) | | 后缀自动机 | Suffix-Automaton | | 使用Tarjan算法求解强连通分量 | Tarjan(Strongly-Connected-Components) | | Treap | Treap | | 数组版的字典树 | Trie(Array) | | 指针版的字典树 | Trie(Pointer) |
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MAX_ELEMENT 100000000 using namespace std; int prime[MAX_ELEMENT], pc, div[MAX_ELEMENT], phi[MAX_ELEMENT]; void sieve(int n) { phi[1] = 1; for (int i = 2; i <= n; i++) { if (div[i] == 0) { prime[pc++] = i; div[i] = i; phi[i] = i - 1; } for (int j = 0; j < pc; j++) { if (i * prime[j] > n) break; div[i * prime[j]] = prime[j]; if (i % prime[j] == 0) { phi[i * prime[j]] = phi[i] * prime[j]; break; } else { phi[i * prime[j]] = phi[i] * (prime[j] - 1); } } } } int main() { int n; scanf("%d", &n); sieve(n); for (int i = 1; i <= n; i++) { printf("phi(%d) = %d\n", i, phi[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n; double a[15][15], s[15]; void gaussian_elimination() { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = n; k >= i; k--) { a[j][k] -= a[i][k] * (a[j][i] / a[i][i]); } } } for (int i = n - 1; i >= 0; i--) { double b = a[i][n]; for (int j = n - 1; j > i; j--) { b -= a[i][j] * s[j]; } s[i] = b / a[i][i]; } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%lf", &a[i][j]); } scanf("%lf", &a[i][n]); } gaussian_elimination(); for (int i = 0; i < n; i++) { printf("x[%d] = %.2lf\n", i, s[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; bool check(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return true; } int main() { int n; while (scanf("%d", &n) > 0) { printf("%s\n", check(n) == true ? "YES" : "NO"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define MAX_HEAP_SIZE 100000 using namespace std; int n, H[MAX_HEAP_SIZE], heapsize; void min_heapify(int i) { int smallest = i; int lch = i << 1; int rch = lch + 1; if (lch <= heapsize && H[smallest] > H[lch]) { smallest = lch; } if (rch <= heapsize && H[smallest] > H[rch]) { smallest = rch; } if (smallest != i) { int temp = H[smallest]; H[smallest] = H[i]; H[i] = temp; min_heapify(smallest); } } void build_heap() { for (int i = heapsize >> 1; i >= 1; i--) { min_heapify(i); } } void insert(int x) { H[++heapsize] = x; int ch = heapsize, p = heapsize >> 1; while (H[p] > H[ch] && p >= 1) { int temp = H[p]; H[p] = H[ch]; H[ch] = temp; ch = p; p >>= 1; } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); insert(x); } // Add more actions here. return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define ELEMENT_COUNT 100000 #define EDITION_COUNT 100000 using namespace std; struct node { int l, r, sum; node *lch, *rch; }*root[EDITION_COUNT]; int n, d[ELEMENT_COUNT], hs = 0; void build(int l, int r, node *u) { u->l = l; u->r = r; if (l == r) { u->sum = d[l]; } else { int mid = (l + r) >> 1; build(l, mid, u->lch = new node); build(mid + 1, r, u->rch = new node); u->sum = u->lch->sum + u->rch->sum; } } int query(int l, int r, node *u) { if (u->l == l && u->r == r) { return u->sum; } else { int mid = (u->l + u->r) >> 1; if (r <= mid) { return query(l, r, u->lch); } else if (l > mid) { return query(l, r, u->rch); } else { return query(l, mid, u->lch) + query(mid + 1, r, u->rch); } } } void modify(int k, int v, node *u, node *ori) { u->l = ori->l; u->r = ori->r; if (u->l == u->r) { u->sum = v; } else { int mid = (u->l + u->r) >> 1; if (k <= mid) { u->rch = ori->rch; u->lch = new node; modify(k, v, u->lch, ori->lch); } else { u->lch = ori->lch; u->rch = new node; modify(k, v, u->rch, ori->rch); } u->sum = u->lch->sum + u->rch->sum; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &d[i]); } build(1, n, root[0] = new node); int in, edition, ql, qr, k, v; while (true) { scanf("%d", &in); if (in == 1) { scanf("%d%d%d", &edition, &ql, &qr); printf("%d\n", query(ql, qr, root[edition])); } else if (in == 2) { scanf("%d%d", &k, &v); hs++; modify(k, v, root[hs] = new node, root[hs - 1]); } else if (in == 3) { printf("Current edition : %d\n", hs); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct point { double x, y; point() { } point(double _x, double _y) : x(_x), y(_y) { } }; struct segment { point s, t; segment() { } segment(point _s, point _t) : s(_s), t(_t) { } }s[110]; double cross(segment base, segment x) { return (base.s.x - base.t.x) * (x.s.y - x.t.y) - (base.s.y - base.t.y) * (x.s.x - x.t.x); } bool segment_intersect(segment x, segment y) { return cross(x, segment(x.s, y.s)) * cross(x, segment(x.s, y.t)) <= 0 && cross(y, segment(y.s, x.s)) * cross(y, segment(y.s, x.t)) <= 0; } int main() { int m; while (true) { scanf("%d", &m); if (m == 0) { break; } for (int i = 0; i < m; i++) { scanf("%lf%lf%lf%lf", &s[i].s.x, &s[i].s.y, &s[i].t.x, &s[i].t.y); } int ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < i; j++) { if (segment_intersect(s[i], s[j]) == true) { ans++; } } } printf("%d\n", ans); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 #define ELEMENT_COUNT 100000 #define EDITION_COUNT 100000 #define POOL_SIZE 1000000 using namespace std; struct node { node *lch, *rch; int v; node(int _v = 0) : v(_v) { lch = rch = NIL; } void* operator new (size_t); }*root[EDITION_COUNT], pool[POOL_SIZE]; int top = 0; int n, d[ELEMENT_COUNT], cur = 0, used = 1; void* node::operator new (size_t) { return pool + top++; } void build(node *&u, int l, int r) { u = new node; if (l == r) { u->v = d[l]; } else { int mid = (l + r) >> 1; build(u->lch, l, mid); build(u->rch, mid + 1, r); } } void modify(node *&u, node *old, int l, int r, int k, int v) { u = new node; if (l == r) { u->v = v; } else { int mid = (l + r) >> 1; if (k <= mid) { u->rch = old->rch; modify(u->lch, old->lch, l, mid, k, v); } else { u->lch = old->lch; modify(u->rch, old->rch, mid + 1, r, k, v); } } } int query(node *u, int l, int r, int k) { if (l == r) { return u->v; } else { int mid = (l + r) >> 1; if (k <= mid) { return query(u->lch, l, mid, k); } else { return query(u->rch, mid + 1, r, k); } } } int main() { int op, e, k, x; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &d[i]); } build(root[cur], 1, n); while (true) { scanf("%d", &op); if (op == 1) { scanf("%d%d", &k, &x); modify(root[used], root[cur], 1, n, k, x); cur = used; used++; } else if (op == 2) { scanf("%d%d", &e, &k); printf("%d\n", query(root[e], 1, n, k)); } else if (op == 3) { scanf("%d", &e); cur = e; } else if (op == 0) { break; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define NIL 0 using namespace std; struct node { node *ch[2]; int key, pr; node(int _key = 0, int _pr = 0) : key(_key), pr(_pr) { ch[0] = ch[1] = NIL; } }*root; void rotate(node *&u, int dir) { node *o = u->ch[dir]; u->ch[dir] = o->ch[dir ^ 1]; o->ch[dir ^ 1] = u; u = o; } int cmp(int ikey, int ukey) { if (ikey == ukey) return -1; return ikey < ukey ? 0 : 1; } void insert(node *&u, int key, int pr) { if (u == NIL) { u = new node(key, pr); } else { int k = cmp(key, u->key); if (k == -1) { return; } insert(u->ch[k], key, pr); if (u->ch[k]->pr > u->pr) { rotate(u, k); } } } int find(node *u, int key) { if (u == NIL) { return -1; } else { int k = cmp(key, u->key); if (k == -1) { return 1; } return find(u->ch[k], key); } } int main() { int in, key; while (true) { scanf("%d", &in); if (in == 1) { scanf("%d", &key); insert(root, key, rand()); } else if (in == 2) { scanf("%d", &key); printf("%d\n", find(root, key)); } else if (in == 0) { break; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define ELEMENT_COUNT 100000 using namespace std; int n, d[ELEMENT_COUNT]; void Shell_sort() { for (int gc = n >> 1; gc >= 1; gc >>= 1) { for (int s = 0; s < gc; s++) { for (int i = s; i < n; i += gc) { for (int j = i - gc; j >= 0 && d[j] > d[j + gc]; j -= gc) { int temp = d[j]; d[j] = d[j + gc]; d[j + gc] = temp; } } } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } Shell_sort(); for (int i = 0; i < n; i++) { printf("%d ", d[i]); } printf("\n"); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MAX_ELEMENT 1000000 using namespace std; int prime[MAX_ELEMENT], pc, div[MAX_ELEMENT]; void sieve(int n) { for (int i = 2; i <= n; i++) { if (div[i] == 0) { prime[pc++] = i; div[i] = i; } for (int j = 0; j < pc; j++) { if (i * prime[j] > n) break; div[i * prime[j]] = prime[j]; if (div[i] == prime[j]) break; } } } int main() { int n; scanf("%d", &n); sieve(n); for (int i = 0; i < pc; i++) { printf("%d ", prime[i]); } printf("\n"); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define ELEMENT_COUNT 1000000 using namespace std; int d[ELEMENT_COUNT]; int flag = 0; void qsort(int l, int r) { if (l < r) { int _rand = rand() % (r - l + 1) + l; int temp = d[_rand]; d[_rand] = d[r]; d[r] = temp; int x = d[r]; int j = l - 1; for (int i = l; i <= r; i++) { if (d[i] <= x) { j++; temp = d[i]; d[i] = d[j]; d[j] = temp; } } qsort(l, j - 1); qsort(j + 1, r); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } qsort(0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", d[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MAX_VERTEX_COUNT 100000 #define MAX_EDGE_COUNT 100000 using namespace std; struct vertex { int first_edge; bool finish; }V[MAX_VERTEX_COUNT]; struct edge { int endp, next; }E[MAX_EDGE_COUNT]; int ec = 1; struct qvertex { int first_edge; }QV[MAX_VERTEX_COUNT]; struct qedge { int endp, next, ans; }QE[MAX_EDGE_COUNT]; int qec = 1; int fa[MAX_VERTEX_COUNT], size[MAX_VERTEX_COUNT], ancestor[MAX_VERTEX_COUNT]; void add_edge(int u, int v) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; ec++; } void add_qedge(int u, int v) { QE[qec].next = QV[u].first_edge; QV[u].first_edge = qec; QE[qec].endp = v; qec++; } void initial_set(int c) { for (int i = 1; i <= c; i++) { fa[i] = i; size[i] = 1; } } int get_root(int x) { if (fa[x] == x) { return x; } return (fa[x] = get_root(fa[x])); } void merge(int x, int y) { int rtx = get_root(x), rty = get_root(y); if (size[rtx] < size[rty]) { fa[rtx] = rty; size[rty] += size[rtx]; } else { fa[rty] = rtx; size[rtx] += size[rty]; } } void DFS(int u, int prev) { ancestor[get_root(u)] = u; for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { if (E[cur].endp != prev) { DFS(E[cur].endp, u); merge(u, E[cur].endp); ancestor[get_root(u)] = u; } } V[u].finish = true; for (int cur = QV[u].first_edge; cur != 0; cur = QE[cur].next) { if (V[QE[cur].endp].finish == true) { QE[cur].ans = ancestor[get_root(QE[cur].endp)]; } } } int main() { int ivc, iqc; scanf("%d%d", &ivc, &iqc); for (int i = 1; i < ivc; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } for (int i = 0; i < iqc; i++) { int u, v; scanf("%d%d", &u, &v); add_qedge(u, v); add_qedge(v, u); } initial_set(ivc); DFS(1, 0); for (int i = 1; i <= ivc; i++) { for (int cur = QV[i].first_edge; cur != 0; cur = QE[cur].next) { if (QE[cur].ans != 0) { printf("The LCA of %d and %d is %d.\n", i, QE[cur].endp, QE[cur].ans); } } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define STRING_LENGTH 300000 using namespace std; struct sortinfo { int x, y, ord; }; int l; char s[STRING_LENGTH], t[STRING_LENGTH / 2]; int rank[STRING_LENGTH * 2], sa[STRING_LENGTH], height[STRING_LENGTH]; void radix_sort(sortinfo *d) { static sortinfo _d[STRING_LENGTH], res[STRING_LENGTH]; static int c[STRING_LENGTH]; memset(c, 0, sizeof(c)); for (int i = 0; i < l; i++) { c[d[i].y]++; } for (int i = 1; i <= l; i++) { c[i] += c[i - 1]; } for (int i = l - 1; i >= 0; i--) { _d[--c[d[i].y]] = d[i]; } memset(c, 0, sizeof(c)); for (int i = 0; i < l; i++) { c[_d[i].x]++; } for (int i = 1; i <= l; i++) { c[i] += c[i - 1]; } for (int i = l - 1; i >= 0; i--) { res[--c[_d[i].x]] = _d[i]; } for (int i = 0; i < l; i++) { d[i] = res[i]; } } void init_rank() { static int c[256]; static sortinfo d[STRING_LENGTH]; int x = 1; for (int i = 0; i < l; i++) { c[(int)s[i]] = 1; } for (int i = 0; i < 256; i++) { if (c[i] == 1) { c[i] = x++; } } for (int i = 0; i < l; i++) { rank[i] = c[(int)s[i]]; } for (int k = 1; k < l; k <<= 1) { for (int i = 0; i < l; i++) { d[i].x = rank[i]; d[i].y = rank[i + k]; d[i].ord = i; } radix_sort(d); x = 1; rank[d[0].ord] = 1; for (int i = 1; i < l; i++) { rank[d[i].ord] = (d[i].x == d[i - 1].x && d[i].y == d[i - 1].y ? x : ++x); } if (x == l) { break; } } } void rank_to_sa() { for (int i = 0; i < l; i++) { sa[rank[i]] = i; } } void init_height() { int k = 0; for (int i = 0; i < l; i++) { if (k > 0) { k--; } if (rank[i] == l) { continue; } for (; s[i + k] == s[sa[rank[i] + 1] + k]; k++) ; height[rank[i]] = k; } } int main() { int l1, l2; scanf("%s%s", s, t); l1 = strlen(s); l2 = strlen(t); s[l1] = '#'; strcat(s, t); l = l1 + l2; init_rank(); rank_to_sa(); init_height(); int maxlen = 0; for (int i = 1; i < l; i++) { if (height[i] > maxlen && ((sa[i] < l1 && sa[i + 1] > l1) || (sa[i] > l1 && sa[i + 1] < l1))) { maxlen = height[i]; } } printf("%d", maxlen); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to <http://unlicense.org>
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define ELEMENT_COUNT 200000 using namespace std; struct node { int l, r; long long sum, lazy; }T[ELEMENT_COUNT * 4]; int n, q; int d[ELEMENT_COUNT]; void build_tree(int l, int r, int k) { T[k].l = l; T[k].r = r; if (l == r) { T[k].sum = d[l]; } else { int mid = (l + r) >> 1; build_tree(l, mid, k << 1); build_tree(mid + 1, r, (k << 1) + 1); T[k].sum = T[k << 1].sum + T[(k << 1) + 1].sum; } } long long get_sum(int l, int r, int k) { if (l == T[k].l && r == T[k].r) { return T[k].sum + T[k].lazy * (r - l + 1); } else { T[k].sum += T[k].lazy * (T[k].r - T[k].l + 1); T[k << 1].lazy += T[k].lazy; T[(k << 1) + 1].lazy += T[k].lazy; T[k].lazy = 0; if (r <= T[k << 1].r) { return get_sum(l, r, k << 1); } else if (l >= T[(k << 1) + 1].l) { return get_sum(l, r, (k << 1) + 1); } else { return get_sum(l, T[k << 1].r, k << 1) + get_sum(T[(k << 1) + 1].l, r, (k << 1) + 1); } } } long long delta; void update_segment(int l, int r, int k) { if (l == T[k].l && r == T[k].r) { T[k].lazy += delta; } else { T[k].sum += delta * (r - l + 1); if (r <= T[k << 1].r) { update_segment(l, r, k << 1); } else if (l >= T[(k << 1) + 1].l) { update_segment(l, r, (k << 1) + 1); } else { update_segment(l, T[k << 1].r, k << 1); update_segment(T[(k << 1) + 1].l, r, (k << 1) + 1); } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } build_tree(0, n - 1, 1); scanf("%d", &q); while (q--) { int o, a, b; long long d; scanf("%d", &o); switch (o) { case 1: scanf("%d%d%lld", &a, &b, &d); delta = d; update_segment(a - 1, b - 1, 1); break; case 2: scanf("%d%d", &a, &b); printf("%lld\n", get_sum(a - 1, b - 1, 1)); break; case 3: for (int i = 0; i < n * 2; i++) { printf("%2d ", i); } printf("\n"); for (int i = 0; i < n * 2; i++) { printf("%2lld ", T[i].lazy); } printf("\n"); break; default: printf("Instruction incorrect!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define NIL 0 using namespace std; struct node { int info; node *next[26]; }; node root; void insert(char *s) { int l = strlen(s); node *cur = &root; for (int i = 0; i < l; i++) { if (cur->next[s[i] - 'a'] == NIL) { node *o = new node; o->info = 0; cur->next[s[i] - 'a'] = o; } cur = cur->next[s[i] - 'a']; } cur->info++; } int find(char *s) { int l = strlen(s); node *cur = &root; for (int i = 0; i < l; i++) { if (cur->next[s[i] - 'a'] == NIL) { return -1; } else { cur = cur->next[s[i] - 'a']; } } return cur->info; } int main() { int in; char str[100]; while (true) { scanf("%d", &in); if (in == 1) { scanf("%s", str); insert(str); } else if (in == 2) { scanf("%s", str); printf("%d\n", find(str)); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define LOWBIT(x) ((x) & (-(x))) #define COMPOSITE 0 #define PRIME 1 typedef long long ll; using namespace std; ll qpow(ll x, int y, int p) { if (y == 0) return 1; ll h = qpow(x * x % p, y >> 1, p); if ((y & 1) == 0) return h; else return h * x % p; } bool witness(int a, int n) { int t = LOWBIT(n - 1); int u = n / t; ll x = qpow(a, u, n); for (int i = 1; i < t; i <<= 1) { ll r = x * x % (ll)n; if (r == 1 && x != 1 && x != n - 1) return true; x = r; } if (x != 1) return true; return false; } bool Miller_Rabin(int n, int s) { if (n == 2) return PRIME; if ((n & 1) == 0) return COMPOSITE; while (s--) { if (witness(rand() % (n - 2) + 2, n) == true) return COMPOSITE; } return PRIME; } int main() { int n; while (true) { scanf("%d", &n); printf("%s\n", Miller_Rabin(n, 10) == COMPOSITE ? "COMPOSITE" : "PRIME"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define VERTEX_COUNT 1010 #define EDGE_COUNT 250010 using namespace std; struct vertex { int first, mv; }V[VERTEX_COUNT]; struct edge { int endp, next; }E[EDGE_COUNT]; int nl, nr, iec, ec = 2; bool vis[VERTEX_COUNT]; inline void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } bool dfs(int u) { if (vis[u] == true) return false; vis[u] = true; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[u].mv != E[cur].endp && (V[E[cur].endp].mv == 0 || dfs(V[E[cur].endp].mv) == true)) { V[u].mv = E[cur].endp; V[E[cur].endp].mv = u; return true; } } return false; } int match() { int res = 0; for (int i = 1; i <= nl; i++) { memset(vis, false, sizeof(vis)); res += (int)dfs(i); } return res; } int main() { int u, v; scanf("%d%d%d", &nl, &nr, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d", &u, &v); add_edge(u, v + nl); } printf("%d\n", match()); for (int i = 1; i <= nl; i++) { if (V[i].mv != 0) printf("%d ", V[i].mv - nl); else printf("0 "); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define MOD 998244353 #define ROOT 3 typedef long long ll; using namespace std; int n, n0, n1, k; ll a0[500000], a1[500000]; inline void swap(ll &x, ll &y) { ll temp = x; x = y; y = temp; } ll qpow(ll x, int y) { if (y == 1) return x; ll t = qpow(x, y >> 1); t = t * t % MOD; if ((y & 1) == 0) return t; else return t * x % MOD; } void pre(ll *a) { static ll temp[500000]; memcpy(temp, a, sizeof(temp)); for (int i = 0; i < n; i++) { int x = 0; for (int p = 0; p < k; p++) { if ((i & (1 << p)) != 0) { x += (1 << (k - p - 1)); } } a[i] = temp[x]; } } void dft(ll *a, int rev) { ll _g = rev == 1 ? ROOT : qpow(ROOT, MOD - 2); for (int i = 2; i <= n; i <<= 1) { ll gn = qpow(_g, (MOD - 1) / i); for (int p = 0; p < n; p += i) { ll g = 1LL; for (int q = p; q < p + (i >> 1); q++) { ll temp = a[q]; a[q] = (a[q] + g * a[q + (i >> 1)]) % MOD; a[q + (i >> 1)] = (temp - g * a[q + (i >> 1)]) % MOD; g = g * gn % MOD; } } } } inline ll read() { char c; do { c = getchar(); } while (c < '0' || c > '9'); return (ll)(c - '0'); } int main() { scanf("%d%d", &n0, &n1); for (int i = 0; i <= n0; i++) { a0[i] = read(); } for (int i = 0; i <= n1; i++) { a1[i] = read(); } n = n0 + n1; while (n > 0) { k++; n >>= 1; } n = 1 << k; pre(a0); pre(a1); dft(a0, 1); dft(a1, 1); for (int i = 0; i < n; i++) { a0[i] = a0[i] * a1[i] % MOD; } pre(a0); dft(a0, -1); ll inv = qpow(n, MOD - 2); for (int i = 0; i <= n0 + n1; i++) { printf("%lld ", (a0[i] * inv % MOD + MOD) % MOD); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #include <algorithm> #define NIL 0 using namespace std; struct data { char id[100]; int pr; }d[50010]; int n, lch[50010], rch[50010], S[50010], sp; inline void parse(char *buf, char *str, int *num) { for (int i = 0; ; i++) { if (buf[i] == '/') { buf[i] = ' '; break; } } sscanf(buf, "%s %d", str, num); } void print_tree(int rt) { if (rt != 0) { printf("("); print_tree(lch[rt]); printf("%s/%d", d[rt].id, d[rt].pr); print_tree(rch[rt]); printf(")"); } } inline bool _data_cmp_(const data &x, const data &y) { return strcmp(x.id, y.id) < 0; } int main() { char temp[1000]; while (true) { scanf("%d", &n); if (n == 0) { break; } for (int i = 1; i <= n; i++) { scanf("%s", temp); parse(temp, d[i].id, &d[i].pr); } sort(d + 1, d + n + 1, _data_cmp_); sp = 0; for (int i = 1; i <= n; i++) { lch[i] = rch[i] = NIL; S[sp] = 0; while (sp > 0 && d[i].pr > d[S[sp - 1]].pr) sp--; if (sp > 0) { rch[S[sp - 1]] = i; } lch[i] = S[sp]; S[sp++] = i; } print_tree(S[0]); printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, q; int w[100][100]; inline int min(int x, int y) { return x < y ? x : y; } void Floyd_Warshall() { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { w[i][j] = min(w[i][j], w[i][k] + w[k][j]); } } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &w[i][j]); } } Floyd_Warshall(); scanf("%d", &q); for (int i = 0; i < q; i++) { int a, b; scanf("%d%d", &a, &b); printf("%d\n", w[a - 1][b - 1]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
// Algorithm: Fibonacci Heap #include <cstdio> #include <cstring> #include <algorithm> #define NIL 0 #define MAX_HEAP_SIZE 100010 using namespace std; class FibonacciHeap { private: struct node { node *child, *parent, *left, *right; int key, degree; bool mark; node(int _key) : child(NIL), parent(NIL), left(NIL), right(NIL), key(_key), degree(0), mark(false) { } }; node *min; size_t heap_size; void _M_insert_to(node *v, node *u); void _M_insert_to_root_list(node *u); void _M_remove_from_root_list(node *u); void _M_link(node *v, node *u); void _M_consolidate(); public: FibonacciHeap(); void insert(int key); int extract_min(); }; void FibonacciHeap::_M_insert_to(node *v, node *u) { v->right = u->right; v->left = u; u->right->left = v; u->right = v; } void FibonacciHeap::_M_insert_to_root_list(node *u) { _M_insert_to(u, min); } void FibonacciHeap::_M_remove_from_root_list(node *u) { u->left->right = u->right; u->right->left = u->left; } void FibonacciHeap::_M_link(node *v, node *u) { _M_remove_from_root_list(v); if (u->child == NIL) { u->child = v; v->parent = u; v->left = v->right = v; } else { _M_insert_to(v, u->child); v->parent = u; } } void FibonacciHeap::_M_consolidate() { static node *root_list[MAX_HEAP_SIZE]; node *first = min; node *u = first; int cnt = 0; do { root_list[cnt++] = u; u = u->right; } while (u != first); node *list[50]; memset(list, 0, sizeof(list)); for (int i = 0; i < cnt; i++) { node *x = root_list[i]; while (list[x->degree] != NIL) { node *y = list[x->degree]; if (x->key > y->key) swap(x, y); _M_link(y, x); list[x->degree++] = NIL; } list[x->degree] = x; } min = NIL; for (int i = 0; i < 50; i++) { if (list[i] != NIL) { if (min == NIL) { min = list[i]; list[i]->left = list[i]->right = list[i]; } else { _M_insert_to_root_list(list[i]); if (list[i]->key < min->key) min = list[i]; } } } } FibonacciHeap::FibonacciHeap() : min(NIL), heap_size(0) { } void FibonacciHeap::insert(int key) { if (min == NIL) { min = new node(key); min->left = min->right = min; } else { node *u = new node(key); _M_insert_to_root_list(u); if (key < min->key) min = u; } heap_size++; } int FibonacciHeap::extract_min() { int res = min->key; if (min != NIL) { if (heap_size == 1) { min = NIL; } else { if (min->child != NIL) { static node *child_list[MAX_HEAP_SIZE]; node *first = min->child; node *u = first; int cnt = 0; do { child_list[cnt++] = u; u = u->right; } while (u != first); for (int i = 0; i < cnt; i++) { _M_insert_to_root_list(child_list[i]); } } _M_remove_from_root_list(min); min = min->right; _M_consolidate(); } } heap_size--; return res; } int main() { FibonacciHeap H; char op[10]; int x; while (true) { scanf("%s", op); if (strcmp(op, "push") == 0) { scanf("%d", &x); H.insert(x); } else if (strcmp(op, "pop") == 0) { printf("%d\n", H.extract_min()); } else { printf("Command not found.\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <climits> #define INF INT_MAX #define ELEMENT_COUNT 100000 #define ELEMENT_RANGE (1 << 17) #define GROUP_RANGE (1 << 2) using namespace std; int n, d[ELEMENT_COUNT]; struct node { int v, next; }A[ELEMENT_COUNT + 1]; int head[ELEMENT_RANGE / GROUP_RANGE], cnt[ELEMENT_RANGE / GROUP_RANGE], vc = 1; void insert(int v) { int group = v / GROUP_RANGE; A[vc].next = head[group]; head[group] = vc; A[vc].v = v; cnt[group]++; vc++; } void bucket_sort() { for (int i = 0; i < n; i++) { insert(d[i]); } int ptr = 0; for (int i = 0; i < ELEMENT_RANGE / GROUP_RANGE; i++) { for (int j = 0; j < cnt[i]; j++) { int minv = INF, ord; for (int cur = head[i]; cur != 0; cur = A[cur].next) { if (A[cur].v < minv) { minv = A[cur].v; ord = cur; } } d[ptr++] = minv; A[ord].v = INF; } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } bucket_sort(); for (int i = 0; i < n; i++) { printf("%d ", d[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int extended_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int _gcd = extended_gcd(b, a % b, x, y); int temp = x; x = y; y = temp - (a / b) * y; return _gcd; } int inverse(int a, int p) { int x, y; extended_gcd(a, p, x, y); return (x % p + p) % p; } int main() { int a, p; while (true) { scanf("%d%d", &a, &p); printf("%d\n", inverse(a, p)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> using namespace std; int data[1000]; void bubble_sort(int *d, int n) { for (int k = 1; k < n; k++) { for (int i = 1; i < n; i++) { if (d[i] < d[i - 1]) { int temp = d[i]; d[i] = d[i - 1]; d[i - 1] = temp; } } } } int main() { for (int i = 0; i < 1000; i++) { data[i] = rand(); } bubble_sort(data, 1000); for (int i = 0; i < 1000; i++) { printf("%d\n", data[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, s[10], sp = 0; bool ins[11]; void DFS() { if (sp == n) { for (int i = 0; i < sp; i++) { printf("%d ", s[i]); } printf("\n"); return; } for (int i = 1; i <= n; i++) { if (ins[i] == false) { ins[i] = true; s[sp++] = i; DFS(); ins[i] = false; sp--; } } } int main() { scanf("%d", &n); DFS(); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define STRING_LENGTH 100000 using namespace std; struct sortinfo { int x, y, ord; }; int l; char s[STRING_LENGTH]; int rank[STRING_LENGTH * 2], sa[STRING_LENGTH]; void radix_sort(sortinfo *d) { static sortinfo _d[STRING_LENGTH], res[STRING_LENGTH]; static int c[STRING_LENGTH]; memset(c, 0, sizeof(c)); for (int i = 0; i < l; i++) { c[d[i].y]++; } for (int i = 1; i <= l; i++) { c[i] += c[i - 1]; } for (int i = l - 1; i >= 0; i--) { _d[--c[d[i].y]] = d[i]; } memset(c, 0, sizeof(c)); for (int i = 0; i < l; i++) { c[_d[i].x]++; } for (int i = 1; i <= l; i++) { c[i] += c[i - 1]; } for (int i = l - 1; i >= 0; i--) { res[--c[_d[i].x]] = _d[i]; } for (int i = 0; i < l; i++) { d[i] = res[i]; } } void init_rank() { static int c[256]; static sortinfo d[STRING_LENGTH]; int x = 1; for (int i = 0; i < l; i++) { c[(int)s[i]] = 1; } for (int i = 0; i < 256; i++) { if (c[i] == 1) { c[i] = x++; } } for (int i = 0; i < l; i++) { rank[i] = c[(int)s[i]]; } for (int k = 1; k < l; k <<= 1) { for (int i = 0; i < l; i++) { d[i].x = rank[i]; d[i].y = rank[i + k]; d[i].ord = i; } radix_sort(d); x = 1; rank[d[0].ord] = 1; for (int i = 1; i < l; i++) { rank[d[i].ord] = (d[i].x == d[i - 1].x && d[i].y == d[i - 1].y ? x : ++x); } if (x == l) { break; } } } void rank_to_sa() { for (int i = 0; i < l; i++) { sa[rank[i] - 1] = i + 1; } } int main() { scanf("%d%s", &l, s); init_rank(); rank_to_sa(); for (int i = 0; i < l; i++) { printf("%d\n", sa[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cmath> using namespace std; struct complex { double re, im; complex(double _re = 0, double _im = 0) : re(_re), im(_im) { } complex operator + (const complex &x) { return complex(re + x.re, im + x.im); } complex operator - (const complex &x) { return complex(re - x.re, im - x.im); } complex operator * (const complex &x) { return complex(re * x.re - im * x.im, re * x.im + im * x.re); } }; int n, m, N, k; complex a[1 << 18], b[1 << 18], res_a[1 << 18], res_b[1 << 18]; int log2(int x) { int res = -1; while (x > 0) { res++; x >>= 1; } return res; } inline int reverse(int x) { int res = 0; for (int i = 0; i <= k; i++) { if ((x & (1 << i)) != 0) { res |= (1 << (k - i)); } } return res; } void init(complex *a, complex *res) { for (int i = 0; i < N; i++) { res[reverse(i)] = a[i]; } } void dft(complex *a, complex *res, int inv) { init(a, res); for (int i = 2; i <= N; i <<= 1) { complex w0(cos(M_PI * 2 / i), inv * sin(M_PI * 2 / i)); for (int j = 0; j < N; j += i) { complex w(1, 0); for (int k = j; k < j + (i >> 1); k++) { complex temp = res[k]; res[k] = temp + res[k + (i >> 1)] * w; res[k + (i >> 1)] = temp - res[k + (i >> 1)] * w; w = w * w0; } } } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) { scanf("%lf", &a[i].re); } for (int i = 0; i <= m; i++) { scanf("%lf", &b[i].re); } k = log2(m + n); N = 1 << (k + 1); dft(a, res_a, 1); dft(b, res_b, 1); for (int i = 0; i < N; i++) { a[i] = res_a[i] * res_b[i]; } dft(a, res_a, -1); for (int i = 0; i <= n + m; i++) { printf("%d ", (int)((res_a[i].re / N) + 0.5)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define STRING_LENGTH 200000 using namespace std; struct sortinfo { int x, y, ord; }; int l; char s[STRING_LENGTH]; int rank[STRING_LENGTH * 2], sa[STRING_LENGTH], height[STRING_LENGTH]; void radix_sort(sortinfo *d) { static sortinfo _d[STRING_LENGTH], res[STRING_LENGTH]; static int c[STRING_LENGTH]; memset(c, 0, sizeof(c)); for (int i = 0; i < l; i++) { c[d[i].y]++; } for (int i = 1; i <= l; i++) { c[i] += c[i - 1]; } for (int i = l - 1; i >= 0; i--) { _d[--c[d[i].y]] = d[i]; } memset(c, 0, sizeof(c)); for (int i = 0; i < l; i++) { c[_d[i].x]++; } for (int i = 1; i <= l; i++) { c[i] += c[i - 1]; } for (int i = l - 1; i >= 0; i--) { res[--c[_d[i].x]] = _d[i]; } for (int i = 0; i < l; i++) { d[i] = res[i]; } } void init_rank() { static int c[256]; static sortinfo d[STRING_LENGTH]; int x = 1; for (int i = 0; i < l; i++) { c[(int)s[i]] = 1; } for (int i = 0; i < 256; i++) { if (c[i] == 1) { c[i] = x++; } } for (int i = 0; i < l; i++) { rank[i] = c[(int)s[i]]; } for (int k = 1; k < l; k <<= 1) { for (int i = 0; i < l; i++) { d[i].x = rank[i]; d[i].y = rank[i + k]; d[i].ord = i; } radix_sort(d); x = 1; rank[d[0].ord] = 1; for (int i = 1; i < l; i++) { rank[d[i].ord] = (d[i].x == d[i - 1].x && d[i].y == d[i - 1].y ? x : ++x); } if (x == l) { break; } } } void rank_to_sa() { for (int i = 0; i < l; i++) { sa[rank[i]] = i; } } void init_height() { int k = 0; for (int i = 0; i < l; i++) { if (k > 0) { k--; } if (rank[i] == l) { continue; } for (; s[i + k] == s[sa[rank[i] + 1] + k]; k++) ; height[rank[i]] = k; } } int main() { scanf("%s", s); l = strlen(s); init_rank(); rank_to_sa(); init_height(); for (int i = 1; i <= l; i++) { printf("%d ", sa[i] + 1); } printf("\n"); for (int i = 1; i < l; i++) { printf("%d ", height[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cmath> #define POINT_COUNT 100000 using namespace std; struct point { int x, y; }P[POINT_COUNT]; struct segment { point begin, end; }D[POINT_COUNT]; int S[POINT_COUNT], sp = 0; int n; void push(int x) { S[sp++] = x; } int pop() { return S[--sp]; } int top() { return S[sp - 1]; } int subtop() { return S[sp - 2]; } int direction(segment &base, segment &s) { return (s.end.x - s.begin.x) * (base.end.y - base.begin.y) - (base.end.x - base.begin.x) * (s.end.y - s.begin.y); } double distance(point &x, point &y) { return sqrt(pow(x.x - y.x, 2) + pow(x.y - y.y, 2)); } void qsort(int l, int r) { if (l < r) { segment x = D[r]; int j = l - 1; for (int i = l; i <= r; i++) { if (direction(x, D[i]) >= 0) { j++; segment temps = D[i]; D[i] = D[j]; D[j] = temps; point tempp = P[i]; P[i] = P[j]; P[j] = tempp; } } qsort(l, j - 1); qsort(j + 1, r); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &P[i].x, &P[i].y); } int p0 = 0; for (int i = 1; i < n; i++) { if (P[i].x < P[p0].x) { p0 = i; } else if (P[i].x == P[p0].x) { if (P[i].y < P[p0].y) { p0 = i; } } } point temp = P[p0]; P[p0] = P[0]; P[0] = temp; for (int i = 1; i < n; i++) { D[i].begin = P[0]; D[i].end = P[i]; } qsort(1, n - 1); push(0); for (int i = 1; i < n; i++) { if (sp >= 2) { if (direction(D[i], D[top()]) == 0) { if (P[i].x > P[top()].x) { pop(); } else { continue; } } int dir; do { segment base, toward; base.begin = P[subtop()]; base.end = P[top()]; toward.begin = P[top()]; toward.end = P[i]; dir = direction(base, toward); if (dir > 0) { pop(); } } while (dir > 0); } push(i); } double res = 0; for (int i = 1; i < sp; i++) { res += distance(P[S[i]], P[S[i - 1]]); } res += distance(P[S[sp - 1]], P[0]); printf("%.1lf", res); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; long long Euler(long long x) { long long prev = 0, res = x; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) { if (i != prev) { res = res / i * (i - 1); prev = i; } x /= i; i = 1; } } if (x != 1 && x != prev) { res = res / x * (x - 1); } return (long long)res; } int main() { while (true) { long long x; scanf("%lld", &x); if (x == 0) { break; } printf("%lld\n", Euler(x)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 #define VERTEX_COUNT 1000 #define EDGE_COUNT 1000 using namespace std; struct vertex { int first, dis; }V[VERTEX_COUNT]; struct edge { int endp, next, flow; }E[EDGE_COUNT]; int ec = 2, iec, ivc, src, sink; inline int min(int x, int y) { return x < y ? x : y; } void add_edge(int u, int v, int f) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; E[ec].flow = f; ec++; } int DFS(int u, int curf) { if (u == sink) { return curf; } int totalf = 0, mindis = INF; for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next) { if (E[cur].flow > 0) { if (V[u].dis == V[E[cur].endp].dis + 1) { int t = DFS(E[cur].endp, min(curf - totalf, E[cur].flow)); E[cur].flow -= t; E[cur ^ 1].flow += t; totalf += t; } mindis = min(mindis, V[E[cur].endp].dis); } } if (totalf == 0) { V[u].dis = mindis + 1; } return totalf; } int max_flow() { int res = 0; while (V[src].dis < ivc) { res += DFS(src, INF); } return res; } int main() { int u, v, f; scanf("%d%d", &iec, &ivc); for (int i = 0; i < iec; i++) { scanf("%d%d%d", &u, &v, &f); add_edge(u, v, f); add_edge(v, u, 0); } src = 1; sink = ivc; printf("%d", max_flow()); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, m; int st[1000010][21], pow2[21]; inline int max(int x, int y) { return x > y ? x : y; } inline int _log2(int x) { int res = 0; while (x > 1) { x >>= 1; res++; } return res; } void init_pow2() { pow2[0] = 1; for (int i = 1; i <= 20; i++) { pow2[i] = pow2[i - 1] * 2; } } void build_st() { init_pow2(); for (int i = 1; i <= 20; i++) { for (int j = 0; j < n; j++) { st[j][i] = max(st[j][i - 1], st[j + pow2[i - 1]][i - 1]); } } } int get_max(int l, int r) { int x = _log2(r - l + 1); return max(st[l][x], st[r - pow2[x] + 1][x]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &st[i][0]); } build_st(); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); printf("%d\n", get_max(a - 1, b - 1)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <queue> typedef long long ll; using namespace std; struct vertex { int first; }V[5010]; struct edge { int endp, next, w; }E[400010]; struct queue_node { int u; ll dis; queue_node(int _u = 0, ll _dis = 0) : u(_u), dis(_dis) { } }; int n, m, ec = 2; bool vis[5010]; inline void add_edge(int u, int v, int w) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; E[ec].w = w; ec++; } inline bool operator < (const queue_node &x, const queue_node &y) { return x.dis > y.dis; } ll prim() { ll res = 0; int vc = 0; priority_queue<queue_node> Q; Q.push(queue_node(1, 0)); while (vc < n) { queue_node u = Q.top(); Q.pop(); if (vis[u.u] == false) { res += u.dis; vis[u.u] = true; vc++; for (int cur = V[u.u].first; cur != 0; cur = E[cur].next) { Q.push(queue_node(E[cur].endp, E[cur].w)); } } } return res; } int main() { scanf("%d%d", &n, &m); int u, v, w; for (int i = 0; i < m; i++) { scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w); add_edge(v, u, w); } printf("%lld\n", prim()); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define MAX_HEAP_SIZE 100000 using namespace std; int n, H[MAX_HEAP_SIZE], heapsize; void min_heapify(int i) { int smallest = i; int lch = i << 1; int rch = lch + 1; if (lch <= heapsize && H[smallest] > H[lch]) { smallest = lch; } if (rch <= heapsize && H[smallest] > H[rch]) { smallest = rch; } if (smallest != i) { int temp = H[smallest]; H[smallest] = H[i]; H[i] = temp; min_heapify(smallest); } } void build_heap() { for (int i = heapsize >> 1; i >= 1; i--) { min_heapify(i); } } int extract_min() { int res = H[1]; H[1] = H[heapsize--]; min_heapify(1); return res; } int main() { scanf("%d", &n); heapsize = n; for (int i = 1; i <= n; i++) { scanf("%d", &H[i]); } build_heap(); while (heapsize > 0) { printf("%d\n", extract_min()); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define ELEMENT_COUNT 100000 using namespace std; // Requirement: every element in stack must be positive. // Otherwise, M[0] should be set to -INF. int S[ELEMENT_COUNT], M[ELEMENT_COUNT]; int sp = 1; void push(int x) { S[sp] = x; if (x > M[sp - 1]) { M[sp] = x; } else { M[sp] = M[sp - 1]; } sp++; } int pop() { return S[--sp]; } bool empty() { return sp == 1; } int get_max() { return M[sp - 1]; } int main() { while (true) { char o[10]; int a; scanf(" %s", o); if (strcmp(o, "push") == 0) { scanf("%d", &a); push(a); } else if (strcmp(o, "pop") == 0) { printf("%d\n", pop()); } else if (strcmp(o, "getmax") == 0) { printf("%d\n", get_max()); } else if (strcmp(o, "abort") == 0) { return 0; } else { printf("No such instruction.\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
// Problem Name: Palindromes // Source: [APIO2014] #include <cstdio> #include <cstring> #define NIL 0 #define ALPHABET_SIZE 26 #define STRING_LENGTH 100000 using namespace std; struct node { node *next[ALPHABET_SIZE], *fail; int start, len; node (int _start = 0, int _len = 0) : fail(NIL), start(_start), len(_len) { memset(next, 0, sizeof(next)); } }*root, *empty, *last; char str[STRING_LENGTH]; int len; void extend(char x, int pos) { node *u = last; while (str[pos] != str[pos - u->len - 1]) u = u->fail; if (u->next[x] == NIL) { node *cur = new node(pos - u->len - 1, u->len + 2); u->next[x] = cur; u = u->fail; if (u == NIL) cur->fail = empty; else { while (str[pos] != str[pos - u->len - 1]) u = u->fail; cur->fail = u->next[x]; } last = cur; } else last = u->next[x]; } void build() { str[0] = '\xFF'; root = new node(0, -1); empty = new node(0, 0); empty->fail = root; last = empty; for (int i = 1; i <= len; i++) { extend(str[i] - 'a', i); } } void print_substring(int l, int r) { printf("\t"); for (int i = l; i <= r; i++) { printf("%c", str[i]); } printf("\n"); } void dfs(node *u) { if (u->len > 0) print_substring(u->start, u->start + u->len - 1); for (int i = 0; i < ALPHABET_SIZE; i++) { if (u->next[i] != NIL) dfs(u->next[i]); } } int main() { scanf("%s", str + 1); len = strlen(str + 1); build(); printf("odd:\n"); dfs(root); printf("even:\n"); dfs(empty); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <queue> #define VERTEX_COUNT 100010 #define EDGE_COUNT 200010 using namespace std; struct vertex { int first, subcnt, fa; bool vis; }V[VERTEX_COUNT]; struct edge { int endp, next; }E[EDGE_COUNT]; int ivc, ec = 2, prufer[VERTEX_COUNT], pc; priority_queue< int, vector<int>, greater<int> > Q; inline void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } void dfs(int u, int fa) { V[u].vis = true; V[u].fa = fa; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].vis == false) dfs(E[cur].endp, u), V[u].subcnt++; } } int main() { int u, v; scanf("%d", &ivc); for (int i = 1; i < ivc; i++) { scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } dfs(1, 0); for (int i = 1; i <= ivc; i++) { if (E[V[i].first].next == 0) Q.push(i); } for (int i = 3; i <= ivc; i++) { int u = Q.top();printf("pop : %d, fa = %d\n", u, V[u].fa); Q.pop(); if (V[u].fa == 0) { for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].fa != 0) { prufer[pc++] = E[cur].endp; V[E[cur].endp].fa = 0; if (V[E[cur].endp].subcnt == 1) Q.push(E[cur].endp); break; } } } else { prufer[pc++] = V[u].fa; V[V[u].fa].subcnt--; if (V[V[u].fa].subcnt == 0) Q.push(V[u].fa); else if (V[V[u].fa].subcnt == 1 && V[V[u].fa].fa == 0) Q.push(V[u].fa); } } for (int i = 0; i < pc; i++) { printf("%d ", prufer[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; long long m; // Calcutate a ^ b % m; long long qpow(long long a, long long b) { if (b == 1) { return a; } long long mid = qpow(a, b >> 1); if ((b & 1) == 0) { return mid * mid % m; } else { return mid * mid % m * a % m; } } int main() { long long a, b; scanf("%lld%lld%lld", &a, &b, &m); printf("%lld", qpow(a, b)); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define VERTEX_COUNT 100000 #define EDGE_COUNT 100000 using namespace std; struct vertex { int first, dfn, low; bool iscv; }V[VERTEX_COUNT]; struct edge { int endp, next; }E[EDGE_COUNT]; int ec = 1, ivc, iec, dfn = 1; inline int min(int x, int y) { return x < y ? x : y; } void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } void DFS(int u, int p) { V[u].dfn = V[u].low = dfn++; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].dfn == 0) { DFS(E[cur].endp, u); V[u].low = min(V[u].low, V[E[cur].endp].low); if (V[E[cur].endp].low >= V[u].dfn) { V[u].iscv = true; } } else if (E[cur].endp != p) { V[u].low = min(V[u].low, V[E[cur].endp].dfn); } } } void get(int rt) { int ctc = 0; V[rt].dfn = V[rt].low = dfn++; for (int cur = V[rt].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].dfn == 0) { ctc++; DFS(E[cur].endp, rt); } } if (ctc > 1) { V[rt].iscv = true; } } int main() { int u, v; scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } for (int i = 1; i <= ivc; i++) { if (V[i].dfn == 0) { get(i); } } int cnt = 0; for (int i = 1; i <= ivc; i++) { if (V[i].iscv == true) { cnt++; } } printf("%d", cnt); }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int extended_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int _gcd = extended_gcd(b, a % b, x, y); int temp = x; x = y; y = temp - (a / b) * y; return _gcd; } int main() { int a, b, x, y; while (true) { scanf("%d%d", &a, &b); int gcd = extended_gcd(a, b, x, y); printf("gcd = %d, x = %d, y = %d.\n", extended_gcd(a, b, x, y), x, y); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 #define N 10010 using namespace std; int n, m; int ch[N][2], fa[N], tf[N]; bool rev[N]; inline void swap(int *x) { int temp = x[0]; x[0] = x[1]; x[1] = temp; } inline int which(int u) { return ch[fa[u]][0] == u ? 0 : 1; } void rotate(int u) { int p = fa[u]; int g = fa[p]; int k1 = which(u), k0 = which(p); int c = ch[u][k1 ^ 1]; fa[c] = p; fa[u] = g; fa[p] = u; ch[g][k0] = u; ch[p][k1] = c; ch[u][k1 ^ 1] = p; } void push_down(int u) { if (rev[u] == true) { swap(ch[u]); rev[u] = false; rev[ch[u][0]] ^= true, rev[ch[u][1]] ^= true; } } void splay(int u, int tar) { static int path[N]; int pc = 0; while (u != tar) { path[pc++] = u; u = fa[u]; } for (int i = pc - 1; i >= 0; i--) { push_down(path[i]); } u = path[0]; while (fa[u] != tar) { if (fa[fa[u]] != tar) { if (which(u) == which(fa[u])) rotate(fa[u]); else rotate(u); } rotate(u); } } int find_top(int u) { push_down(u); while (ch[u][0] != NIL) { u = ch[u][0]; push_down(u); } splay(u, NIL); return u; } int get_root(int u) { while (fa[u] != NIL) { u = fa[u]; } return u; } void access(int u) { int c = NIL; while (u != NIL) { splay(u, NIL); fa[ch[u][1]] = NIL; if (ch[u][1] != NIL) tf[find_top(ch[u][1])] = u; ch[u][1] = c; fa[c] = u; c = find_top(u); u = tf[c]; } } void change_root(int u) { access(u); splay(u, NIL); rev[u] = true; find_top(u); tf[u] = NIL; } void link(int u, int v) { change_root(v); tf[v] = u; } void cut(int u, int v) { change_root(u); access(v); splay(u, NIL); fa[v] = NIL; ch[u][1] = NIL; tf[v] = NIL; } bool query(int u, int v) { change_root(u); access(v); if (get_root(u) == get_root(v)) return true; else return false; } int main() { char op[10]; int u, v; scanf("%d%d", &n, &m); while (m--) { scanf("%s%d%d", op, &u, &v); if (op[0] == 'C') { link(u, v); } else if (op[0] == 'D') { cut(u, v); } else { printf("%s\n", query(u, v) == true ? "Yes" : "No"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, d[100000], b[32], bsize; void linear_basis() { for (int i = 0; i < n; i++) { for (int j = 30; j >= 0 && d[i] != 0; j--) { if ((d[i] >> j) == 0) continue; else if (b[j] != 0) d[i] ^= b[j]; else { b[j] = d[i]; bsize++; break; } } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } linear_basis(); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <algorithm> #define NIL 0 #define INF 1000000000 #define VERTEX_COUNT 100000 #define EDGE_COUNT 200000 #define POOL_SIZE 200000 using namespace std; struct edge; struct vertex { edge *first; int w, depth, size, ord; vertex *p, *heavy, *top; }*nil, V[VERTEX_COUNT]; struct edge { edge *next; vertex *endp; edge(edge *_next = NIL, vertex *_endp = nil); void* operator new (size_t); }E[EDGE_COUNT]; edge::edge(edge *_next, vertex *_endp) : next(_next), endp(_endp) { } int Etop = 0; void* edge::operator new (size_t) { return E + Etop++; } struct node { node *lch, *rch; int sum, max; node(); void* operator new (size_t); }*root, pool[POOL_SIZE]; node::node() : lch(NIL), rch(NIL), sum(0), max(0) { } int pooltop = 0; void* node::operator new (size_t) { return pool + pooltop++; } int ivc, m, ord = 0; vertex *map[VERTEX_COUNT]; void add_edge(vertex *u, vertex *v) { u->first = new edge(u->first, v); } void DFS1(vertex *u, vertex *p, int depth) { vertex *heavy = nil; u->p = p; u->depth = depth; u->size = 1; for (edge *cur = u->first; cur != NIL; cur = cur->next) { if (cur->endp != p) { DFS1(cur->endp, u, depth + 1); u->size += cur->endp->size; if (cur->endp->size > heavy->size) { heavy = cur->endp; } } } u->heavy = heavy; } void DFS2(vertex *u, vertex *top) { u->top = top; u->ord = ++ord; map[ord] = u; if (u->heavy != nil) { DFS2(u->heavy, top); } for (edge *cur = u->first; cur != NIL; cur = cur->next) { if (cur->endp != u->heavy && cur->endp != u->p) { DFS2(cur->endp, cur->endp); } } } void build(node *&u, int l, int r) { u = new node; if (l == r) { u->sum = u->max = map[l]->w; } else { int mid = (l + r) >> 1; build(u->lch, l, mid); build(u->rch, mid + 1, r); u->sum = u->lch->sum + u->rch->sum; u->max = max(u->lch->max, u->rch->max); } } void modify(node *u, int l, int r, int k, int x) { if (l == r) { u->sum = u->max = x; } else { int mid = (l + r) >> 1; if (k <= mid) { modify(u->lch, l, mid, k, x); } else { modify(u->rch, mid + 1, r, k, x); } u->sum = u->lch->sum + u->rch->sum; u->max = max(u->lch->max, u->rch->max); } } int query_sum(node *u, int l, int r, int ql, int qr) { if (l == ql && r == qr) { return u->sum; } int mid = (l + r) >> 1; if (qr <= mid) { return query_sum(u->lch, l, mid, ql, qr); } else if (ql > mid) { return query_sum(u->rch, mid + 1, r, ql, qr); } else { return query_sum(u->lch, l, mid, ql, mid) + query_sum(u->rch, mid + 1, r, mid + 1, qr); } } int query_max(node *u, int l, int r, int ql, int qr) { if (l == ql && r == qr) { return u->max; } int mid = (l + r) >> 1; if (qr <= mid) { return query_max(u->lch, l, mid, ql, qr); } else if (ql > mid) { return query_max(u->rch, mid + 1, r, ql, qr); } else { return max(query_max(u->lch, l, mid, ql, mid), query_max(u->rch, mid + 1, r, mid + 1, qr)); } } void tmodify(vertex *x, int v) { modify(root, 1, ivc, x->ord, v); } int tquery_sum(vertex *x, vertex *y) { int res = 0; vertex *ux, *uy; while (x->top != y->top) { ux = x->top == x ? x->p : x->top; uy = y->top == y ? y->p : y->top; if (ux->depth < uy->depth) { swap(x, y); } if (x->top == x) { res += query_sum(root, 1, ivc, x->ord, x->ord); x = x->p; } else { res += query_sum(root, 1, ivc, x->top->ord + 1, x->ord); x = x->top; } } if (x->depth > y->depth) { swap(x, y); } res += query_sum(root, 1, ivc, x->ord, y->ord); return res; } int tquery_max(vertex *x, vertex *y) { int res = -INF; vertex *ux, *uy; while (x->top != y->top) { ux = x->top == x ? x->p : x->top; uy = y->top == y ? y->p : y->top; if (ux->depth < uy->depth) { swap(x, y); } if (x->top == x) { res = max(res, query_max(root, 1, ivc, x->ord, x->ord)); x = x->p; } else { res = max(res, query_max(root, 1, ivc, x->top->ord + 1, x->ord)); x = x->top; } } if (x->depth > y->depth) { swap(x, y); } res = max(res, query_max(root, 1, ivc, x->ord, y->ord)); return res; } int main() { int u, v, a, b; char op[10]; scanf("%d", &ivc); for (int i = 1; i < ivc; i++) { scanf("%d%d", &u, &v); add_edge(V + u, V + v); add_edge(V + v, V + u); } for (int i = 1; i <= ivc; i++) { scanf("%d", &(V + i)->w); } nil = new vertex; nil->size = 0; DFS1(V + 1, nil, 1); DFS2(V + 1, V + 1); build(root, 1, ivc); scanf("%d", &m); while (m--) { scanf("%s%d%d", op, &a, &b); switch (op[1]) { case 'H': tmodify(V + a, b); break; case 'S': printf("%d\n", tquery_sum(V + a, V + b)); break; case 'M': printf("%d\n", tquery_max(V + a, V + b)); break; } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define VERTEX_COUNT 100000 #define EDGE_COUNT 100000 using namespace std; struct vertex { int first_edge; int ftime; }V[VERTEX_COUNT]; int latest = 1; struct edge { int endp, next; }E[EDGE_COUNT]; int ec = 1; void add_edge(int u, int v) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; ec++; } void DFS(int u) { if (V[u].ftime == 0) { for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { DFS(E[cur].endp); } V[u].ftime = latest++; } } int main() { int ivc, iec; scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); } DFS(1); for (int i = 1; i <= ivc; i++) { printf("%d\n", V[i].ftime); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, d[1000010], least[1000010], longest; int binary_search(int x) { int l = 0, r = longest - 1; do { int mid = (l + r) >> 1; if (least[mid] >= x) { r = mid; } else { l = mid + 1; } } while (l < r); return l; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } least[longest++] = d[0]; for (int i = 1; i < n; i++) { int l = binary_search(d[i]); if (d[i] > least[l]) { least[longest++] = d[i]; } else { least[l] = d[i]; } } printf("%d", longest); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
// Problem Name: [HNOI2010] 弹飞绵羊 Bounce #include <cstdio> #define NIL 0 #define NODE_COUNT 200010 using namespace std; class link_cut_tree { private: int ch[NODE_COUNT][2], fa[NODE_COUNT], size[NODE_COUNT], tf[NODE_COUNT]; public: link_cut_tree(); int get_size(int u); void set_tf(int u, int uf); int which(int u); void update(int u); void rotate(int u); void splay(int u, int tar); int find_top(int u); void access(int u); void link(int u, int v); void cut(int u); void print_fa(); }; int n, m; link_cut_tree::link_cut_tree() { for (int i = 1; i <= n + 1; i++) { size[i] = 1; } } int link_cut_tree::get_size(int u) { return size[u]; } void link_cut_tree::set_tf(int u, int uf) { tf[u] = uf; } int link_cut_tree::which(int u) { return ch[fa[u]][0] == u ? 0 : 1; } void link_cut_tree::update(int u) { size[u] = size[ch[u][0]] + size[ch[u][1]] + 1; } void link_cut_tree::rotate(int u) { int uk = which(u); int p = fa[u]; int q = fa[p]; int c = ch[u][uk ^ 1]; int pk = which(p); fa[c] = p; fa[u] = q; fa[p] = u; ch[p][uk] = c; ch[u][uk ^ 1] = p; ch[q][pk] = u; update(p); update(u); } void link_cut_tree::splay(int u, int tar) { while (fa[u] != tar) { if (fa[fa[u]] != tar) { if (which(u) == which(fa[u])) rotate(fa[u]); else rotate(u); } rotate(u); } } int link_cut_tree::find_top(int u) { int tar = fa[u]; while (ch[u][0] != NIL) { u = ch[u][0]; } splay(u, tar); return u; } void link_cut_tree::access(int u) { int top = NIL; do { splay(u, NIL); size[u] -= size[ch[u][1]]; fa[ch[u][1]] = NIL; ch[u][1] = top; fa[top] = u; update(u); top = find_top(u); u = tf[top]; } while (u != NIL); } void link_cut_tree::link(int u, int v) { tf[v] = u; } void link_cut_tree::cut(int u) { access(tf[u]); tf[u] = NIL; } void link_cut_tree::print_fa() { for (int i = 1; i <= n + 1; i++) { printf("fa[%d] = %d\n", i, fa[i]); } } int main() { int op, k, x; scanf("%d", &n); static link_cut_tree lct; for (int i = 1; i <= n; i++) { scanf("%d", &x); lct.set_tf(i, i + x > n ? n + 1 : i + x); } scanf("%d", &m); while (m--) { scanf("%d", &op); if (op == 1) { scanf("%d", &k); lct.access(k + 1); printf("%d\n", lct.get_size(n + 1) - 1); } else { scanf("%d%d", &k, &x); lct.cut(k + 1); lct.link(k + x > n ? n + 1 : k + 1 + x, k + 1); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int A[100][100], B[100][100], R[100][100]; // Matrix A is p columns and m rows. Matrix B is n columns and p rows. void matrix_multiplication(int p, int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < p; k++) { R[i][j] += A[i][k] * B[k][j]; } } } } int main() { int p, m, n; scanf("%d%d%d", &p, &m, &n); for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { scanf("%d", &A[i][j]); } } for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { scanf("%d", &B[i][j]); } } matrix_multiplication(p, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", R[i][j]); } printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 #define ELEMENT_COUNT 100000 using namespace std; int ch[ELEMENT_COUNT][2], fa[ELEMENT_COUNT], key[ELEMENT_COUNT], size[ELEMENT_COUNT], use = 0, root; inline int which(int u) { return ch[fa[u]][0] == u ? 0 : 1; } inline void update(int u) { size[u] = size[ch[u][0]] + size[ch[u][1]] + 1; } void rotate(int u) { int p = fa[u]; int q = fa[p]; int uk = which(u), pk = which(p); int c = ch[u][uk ^ 1]; fa[c] = p; fa[u] = q; fa[p] = u; ch[p][uk] = ch[u][uk ^ 1]; ch[u][uk ^ 1] = p; ch[q][pk] = u; update(p); update(u); } void splay(int u, int tar) { while (fa[u] != tar) { if (fa[fa[u]] != tar) { if (which(u) == which(fa[u])) rotate(fa[u]); else rotate(u); } rotate(u); } if (tar == 0) root = u; } int cmp(int ikey, int ukey) { return ikey < ukey ? 0 : 1; } void insert(int ikey) { if (root == NIL) root = ++use, ch[use][0] = ch[use][1] = fa[use] = NIL, key[use] = ikey; else { int u = root; while (key[u] != ikey) { int k = cmp(ikey, key[u]); if (k == -1) break; if (ch[u][k] == NIL) ch[u][k] = ++use, fa[use] = u, key[use] = ikey; u = ch[u][k]; } splay(u, 0); } } int find(int fkey) { int u = root; while (u != NIL) { if (key[u] == fkey) return 1; else u = ch[u][cmp(fkey, key[u])]; } return 0; } int main() { int op, x; while (true) { scanf("%d%d", &op, &x); if (op == 1) { insert(x); } else if (op == 2) { splay(x, 0); } else if (op == 3) { printf("find result = %d\n", find(x)); } else if (op == 0) { break; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define TREE_SIZE 100000 using namespace std; struct node { int lch, rch, key, satellite; }T[TREE_SIZE]; int tc = 1; int _satellite; void insert(int rt, int x) { if (T[rt].lch == 0 && T[rt].rch == 0) { T[rt].lch = tc++; T[rt].rch = tc++; T[rt].key = x; T[rt].satellite = _satellite; } else { if (x < T[rt].key) { insert(T[rt].lch, x); } else { insert(T[rt].rch, x); } } } int query(int rt, int key) { if (T[rt].key == key) { return T[rt].satellite; } else { return key < T[rt].key ? query(T[rt].lch, key) : query(T[rt].rch, key); } } int main() { int ord, key, sat; while (true) { scanf("%d", &ord); if (ord == 1) { scanf("%d%d", &key, &sat); _satellite = sat; insert(0, key); } else if (ord == 2) { scanf("%d", &key); printf("%d\n", query(0, key)); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct vertex { int first, dfn, low, col; }V[2010]; struct edge { int endp, next; }E[4000000]; int ivc, iec, ec = 1, dfn = 1, col = 1, S[2010], sp = 0; bool ins[2010]; inline int min(int x, int y) { return x < y ? x : y; } void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } void DFS(int u) { S[sp++] = u; ins[u] = true; V[u].dfn = V[u].low = dfn++; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].dfn == 0) { DFS(E[cur].endp); } if (ins[E[cur].endp] == true && V[E[cur].endp].low < V[u].low) { V[u].low = V[E[cur].endp].low; } } if (V[u].low == V[u].dfn) { int temp; do { temp = S[--sp]; ins[temp] = false; V[temp].col = col; } while (temp != u); col++; } } int main() { int x, y, c; char op[10]; scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d%d%s", &x, &y, &c, op); switch (op[0]) { case 'A': if (c == 0) { add_edge(x, y + ivc); add_edge(y, x + ivc); } else { add_edge(x + ivc, x); add_edge(y + ivc, y); } break; case 'O': if (c == 0) { add_edge(x, x + ivc); add_edge(y, y + ivc); } else { add_edge(x + ivc, y); add_edge(y + ivc, x); } break; case 'X': if (c == 0) { add_edge(x, y); add_edge(y, x); add_edge(x + ivc, y + ivc); add_edge(y + ivc, x + ivc); } else { add_edge(x, y + ivc); add_edge(y, x + ivc); add_edge(x + ivc, y); add_edge(y + ivc, x); } break; } } for (int i = ivc * 2 - 1; i >= 0; i--) { if (V[i].dfn == 0) { DFS(i); } } for (int i = 0; i < ivc; i++) { if (V[i].col == V[i + ivc].col) { printf("NO"); return 0; } } printf("YES"); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int sc, rc; bool sel[50010]; // Take m stone(s) last n stone(s). void enum_combination(int n, int m) { if (m == 0) { for (int i = 0; i < sc; i++) { if (sel[i]) { printf("%d ", i); } } printf("\n"); return; } if (n > m) { sel[sc - n - 1] = true; enum_combination(n - 1, m - 1); sel[sc - n - 1] = false; enum_combination(n - 1, m); } else { sel[sc - n - 1] = true; enum_combination(n - 1, m - 1); } sel[sc - n - 1] = false; } int main() { scanf("%d%d", &sc, &rc); sc += 2; enum_combination(sc - 2, rc); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct point { double x, y; point(double _x = 0, double _y = 0) : x(_x), y(_y) { } }d[1000010]; int t, n; inline double abs(double x) { return x < 0 ? -x : x; } inline point operator - (const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } inline double cross(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } inline double area(const point &a, const point &b, const point &c) { return cross(b - a, c - a) / 2.0; } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf%lf", &d[i].x, &d[i].y); } double sumx = 0, sumy = 0, areasum = 0; for (int i = 2; i < n; i++) { double a = area(d[0], d[i - 1], d[i]); sumx += (d[0].x + d[i - 1].x + d[i].x) / 3.0f * a; sumy += (d[0].y + d[i - 1].y + d[i].y) / 3.0f * a; areasum += a; } printf("%.2lf %.2lf\n", sumx / areasum + 1e-8, sumy / areasum + 1e-8); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, S[100][100]; void init_S() { for (int i = 1; i <= n; i++) { S[i][0] = 0; S[0][i] = 0; } S[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { S[i][j] = S[i - 1][j - 1] + S[i - 1][j] * j; } } } int main() { scanf("%d", &n); init_S(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { printf("%5d ", S[i][j]); } printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define MAX_ELEMENT_COUNT 1000000 using namespace std; int d[MAX_ELEMENT_COUNT]; void qsort(int l, int r) { if (l < r) { int x = d[r]; int j = l - 1; for (int i = l; i <= r; i++) { if (d[i] <= x) { j++; int temp = d[i]; d[i] = d[j]; d[j] = temp; } } qsort(l, j - 1); qsort(j + 1, r); } } int main() { for (int i = 0; i < MAX_ELEMENT_COUNT; i++) { d[i] = rand(); } qsort(0, MAX_ELEMENT_COUNT - 1); for (int i = 0; i < MAX_ELEMENT_COUNT; i++) { printf("%d\n", d[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #define NIL 0 #define EDITION_COUNT 100000 using namespace std; struct node { node *ch[2]; int key, pr; node(int _key = 0, int _pr = 0) : key(_key), pr(_pr) { } }*root[EDITION_COUNT]; int hs = 0; void rotate(node *&u, int dir) { node *o = u->ch[dir]; u->ch[dir] = o->ch[dir ^ 1]; o->ch[dir ^ 1] = u; u = o; } int cmp(int ikey, int ukey) { if (ikey == ukey) return -1; return ikey < ukey ? 0 : 1; } void insert(node *&u, node *prev, int key, int pr) { if (prev == NIL) { u = new node(key, pr); return; } u = new node(prev->key, prev->pr); int k = cmp(key, prev->key); u->ch[k ^ 1] = prev->ch[k ^ 1]; if (k == -1) { u->ch[k] = prev->ch[k]; return; } insert(u->ch[k], prev->ch[k], key, pr); if (u->ch[k]->pr > u->pr) { rotate(u, k); } } int find(node *&u, int key) { if (u == NIL) { return -1; } else { int k = cmp(key, u->key); if (k == -1) { return 1; } return find(u->ch[k], key); } } int main() { int in, edition, key; while (true) { scanf("%d", &in); if (in == 1) { scanf("%d", &key); insert(root[hs + 1], root[hs], key, rand()); hs++; } else if (in == 2) { scanf("%d%d", &edition, &key); printf("%d\n", find(root[edition], key)); } else if (in == 3) { printf("Current edition : %d\n", hs); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 using namespace std; struct node { int key, value; node *ch[2]; node(int _key = 0, int _value = 0) : key(_key), value(_value) { ch[0] = ch[1] = NIL; } }*root; void rotate(node *&u, int dir) { node *o = u->ch[dir]; u->ch[dir] = o->ch[dir ^ 1]; o->ch[dir ^ 1] = u; u = o; } void insert(node *&u, int key, int value) { if (u == NIL) { u = new node(key, value); return; } if (key < u->key) { insert(u->ch[0], key, value); rotate(u, 0); } else if (key > u->key) { insert(u->ch[1], key, value); rotate(u, 1); } } int find(node *&u, int key) { if (u == NIL) { return -1; } if (u->key == key) { return u->value; } int res; if (key < u->key) { res = find(u->ch[0], key); if (u->ch[0] != NIL) rotate(u, 0); } else if (key > u->key) { res = find(u->ch[1], key); if (u->ch[1] != NIL) rotate(u, 1); } return res; } int main() { int in, key, value; while (true) { scanf("%d", &in); if (in == 1) { scanf("%d%d", &key, &value); insert(root, key, value); } else if (in == 2) { scanf("%d", &key); printf("%d\n", find(root, key)); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define ELEMENT_COUNT 1000 using namespace std; int n, d[ELEMENT_COUNT]; void insertion_sort() { for (int i = 1; i < n; i++) { for (int j = i; j > 0; j--) { if (d[j] < d[j - 1]) { int temp = d[j]; d[j] = d[j - 1]; d[j - 1] = temp; } else { break; } } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } insertion_sort(); for (int i = 0; i < n; i++) { printf("%d\n", d[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 #define VERTEX_COUNT 100010 #define EDGE_COUNT 1000010 using namespace std; struct vertex { int first_edge; int dis, hpos; }V[VERTEX_COUNT]; struct edge { int endp, next; int w; }E[EDGE_COUNT]; int ec = 1; int ivc, iec; int H[VERTEX_COUNT], pos[VERTEX_COUNT]; int heapsize; void add_edge(int u, int v, int w) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; E[ec].w = w; ec++; } void initial_single_source(int s) { for (int i = 1; i <= ivc; i++) { V[i].dis = INF; } V[s].dis = 0; } void build_heap(int s) { heapsize = ivc; for (int i = 1; i <= ivc; i++) { H[i] = i; pos[i] = i; } H[s] = 1; pos[1] = s; H[1] = s; pos[s] = 1; } void heap_sink(int i) { int lch = i << 1; int rch = lch + 1; int smallest = i; if (lch <= heapsize && V[H[lch]].dis < V[H[smallest]].dis) { smallest = lch; } if (rch <= heapsize && V[H[rch]].dis < V[H[smallest]].dis) { smallest = rch; } if (smallest != i) { V[H[smallest]].hpos = i; V[H[i]].hpos = smallest; int temp = H[i]; H[i] = H[smallest]; H[smallest] = temp; heap_sink(smallest); } } void heap_float(int i) { int p = i >> 1; while (p >= 1 && V[H[i]].dis < V[H[p]].dis) { pos[H[i]] = p; pos[H[p]] = i; int temp = H[i]; H[i] = H[p]; H[p] = temp; i = p; p = i >> 1; } } int extract_min() { int res = H[1]; H[1] = H[heapsize--]; pos[H[1]] = 1; heap_sink(1); return res; } void Dijkstra(int s) { initial_single_source(s); build_heap(s); while (heapsize > 0) { int u = extract_min(); for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { int newpath = V[u].dis + E[cur].w; if (newpath < V[E[cur].endp].dis) { V[E[cur].endp].dis = newpath; heap_float(pos[E[cur].endp]); } } } } int main() { scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w); } Dijkstra(1); printf("%d", V[ivc].dis); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; int n, su[100][100]; void init_su() { for (int i = 1; i <= n; i++) { su[i][0] = 0; su[0][i] = 0; } su[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { su[i][j] = su[i - 1][j - 1] + su[i - 1][j] * (i - 1); } } } int main() { scanf("%d", &n); init_su(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { printf("%5d ", su[i][j]); } printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct node { int l, r, min, lazy; }T[4000010]; int d[1000010]; inline int min(int x, int y) { return x < y ? x : y; } void build_tree(int l, int r, int k) { T[k].l = l; T[k].r = r; if (l == r) { T[k].min = d[l]; } else { int mid = (l + r) >> 1; build_tree(l, mid, k << 1); build_tree(mid + 1, r, (k << 1) + 1); T[k].min = min(T[k << 1].min, T[(k << 1) + 1].min); } } int delta; void update(int l, int r, int k) { if (T[k].l == l && T[k].r == r) { T[k].lazy += delta; T[k].min += delta; } else { int mid = (T[k].l + T[k].r) >> 1; if (r <= mid) { update(l, r, k << 1); } else if (l > mid) { update(l, r, (k << 1) + 1); } else { update(l, mid, k << 1); update(mid + 1, r, (k << 1) + 1); } T[k].min = min(T[k << 1].min, T[(k << 1) + 1].min); } } int query(int l, int r, int k) { if (T[k].l == l && T[k].r == r) { return T[k].min; } else { int mid = (T[k].l + T[k].r) >> 1, res; T[k << 1].lazy += T[k].lazy; T[k << 1].min += T[k].lazy; T[(k << 1) + 1].lazy += T[k].lazy; T[(k << 1) + 1].min += T[k].lazy; T[k].lazy = 0; if (r <= mid) { res = query(l, r, k << 1); } else if (l > mid) { res = query(l, r, (k << 1) + 1); } else { res = min(query(l, mid, k << 1), query(mid + 1, r, (k << 1) + 1)); } T[k].min = min(T[k << 1].min, T[(k << 1) + 1].min); return res; } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &d[i]); } build_tree(1, n, 1); while (true) { int o, a, b, d; scanf("%d", &o); switch (o) { case 1: scanf("%d%d", &a, &b); printf("%d\n", query(a, b, 1)); break; case 2: scanf("%d%d%d", &a, &b, &d); delta = d; update(a, b, 1); break; } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MAX_ELEMENT_COUNT 1000 int n, d[MAX_ELEMENT_COUNT]; void selection_sort() { for (int i = 0; i < n; i++) { int minv = d[i], minp = i; for (int j = i + 1; j < n; j++) { if (d[j] < minv) { minv = d[j]; minp = j; } } int t = d[i]; d[i] = minv; d[minp] = t; } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", d + i); } selection_sort(); for (int i = 0; i < n; i++) { printf("%d ", d[i]); } printf("\n"); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstdlib> #include <algorithm> #define ELEMENT_COUNT 1000000 using namespace std; int d[ELEMENT_COUNT]; int flag = 0; void qsort(int l, int r) { if (l < r) { int _rand = rand() % (r - l + 1) + l; int temp = d[_rand]; d[_rand] = d[r]; d[r] = temp; int x = d[r]; int j = l - 1; for (int i = l; i < r; i++) { if (d[i] < x) { j++; temp = d[i]; d[i] = d[j]; d[j] = temp; } else if (d[i] == x) { if ((flag++ & 1) == 0) { j++; temp = d[i]; d[i] = d[j]; d[j] = temp; } } } j++; temp = d[r]; d[r] = d[j]; d[j] = temp; qsort(l, j - 1); qsort(j + 1, r); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } qsort(0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", d[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define VERTEX_COUNT 210 #define EDGE_COUNT 20000 using namespace std; struct vertex { int first_edge; int pree, prev; }V[VERTEX_COUNT]; bool vis[VERTEX_COUNT]; struct edge { int endp, next; int flow; }E[EDGE_COUNT]; int ec = 2; int ivc, iec; int maxflow = 0; int Q[VERTEX_COUNT]; int hp = 0, tp = 0; inline int min(int x, int y) { return x < y ? x : y; } void add_edge(int u, int v, int f) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; E[ec].flow = f; ec++; } inline void enqueue(int x) { Q[tp++] = x; } inline int dequeue() { return Q[hp++]; } inline bool isempty() { return hp == tp; } inline void empty() { hp = tp = 0; } void Edmonds_Karp(int s, int t) { bool has_ap; do { has_ap = false; memset(vis, false, sizeof(vis)); empty(); enqueue(s); while (!isempty()) { int u = dequeue(); for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { if (E[cur].flow > 0) { if (E[cur].endp == t) { has_ap = true; int p = u, pathmin = E[cur].flow; while (p != 1) { pathmin = min(pathmin, E[V[p].pree].flow); p = V[p].prev; } p = u; E[cur].flow -= pathmin; E[cur ^ 1].flow += pathmin; while (p != 1) { E[V[p].pree].flow -= pathmin; E[V[p].pree ^ 1].flow += pathmin; p = V[p].prev; } maxflow += pathmin; goto END; } else if (vis[E[cur].endp] == false) { V[E[cur].endp].pree = cur; V[E[cur].endp].prev = u; vis[E[cur].endp] = true; enqueue(E[cur].endp); } } } } END:; } while (has_ap == true); } int main() { // e.g. CodeVS - 1993. scanf("%d%d", &iec, &ivc); for (int i = 0; i < iec; i++) { int u, v, f; scanf("%d%d%d", &u, &v, &f); add_edge(u, v, f); add_edge(v, u, 0); } Edmonds_Karp(1, ivc); printf("%d", maxflow); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 using namespace std; struct vertex { int first, dis; }V[1010]; struct edge { int endp, next, flow; }E[502010]; int nl, nr, iec, ec = 2, src = 1005, sink = 1006, gap[1010]; inline int min(int x, int y) { return x < y ? x : y; } void init() { gap[0] = nl + nr + 2; } void add_edge(int u, int v, int f) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; E[ec].flow = f; ec++; } int isap(int u, int curf) { if (u == sink) { return curf; } int totalf = 0, mindis = nl + nr + 2; for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next) { if (E[cur].flow > 0) { if (V[u].dis == V[E[cur].endp].dis + 1) { int f = isap(E[cur].endp, min(curf - totalf, E[cur].flow)); E[cur].flow -= f; E[cur ^ 1].flow += f; totalf += f; } if (V[E[cur].endp].dis < mindis) { mindis = V[E[cur].endp].dis; } } } if (totalf == 0) { if (--gap[V[u].dis] == 0) V[src].dis = nl + nr + 2; V[u].dis = mindis + 1; gap[V[u].dis]++; } return totalf; } int max_flow() { int res = 0; while (V[src].dis < nl + nr + 2) { res += isap(src, INF); } return res; } int main() { int u, v; scanf("%d%d%d", &nl, &nr, &iec); for (int i = 0; i < iec; i++) { scanf("%d%d", &u, &v); add_edge(u, v + nl, 1); add_edge(v + nl, u, 0); } for (int i = 1; i <= nl; i++) { add_edge(src, i, 1); add_edge(i, src, 0); } for (int i = 1; i <= nr; i++) { add_edge(i + nl, sink, 1); add_edge(sink, i + nl, 0); } init(); printf("%d\n", max_flow()); for (int i = 1; i <= nl; i++) { bool has = false; for (int cur = V[i].first; cur != 0; cur = E[cur].next) { if (E[cur].endp != src && E[cur].flow == 0) { printf("%d ", E[cur].endp - nl); has = true; break; } } if (has == false) { printf("0 "); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define INF 1000000000 #define VERTEX_COUNT 10000 #define EDGE_COUNT 100000 using namespace std; struct vertex { int first, prev, pree, maxflow; bool inq; }V[VERTEX_COUNT]; struct edge { int endp, next, flow; }E[EDGE_COUNT]; int ec = 2, n, m, ans; int Q[VERTEX_COUNT], hp = 0, tp = 0; void enqueue(int x) { Q[tp++] = x; tp %= VERTEX_COUNT; } int dequeue() { hp %= VERTEX_COUNT; return Q[hp++]; } void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; E[ec].flow = 1; ec++; E[ec].next = V[v].first; V[v].first = ec; E[ec].endp = u; E[ec].flow = 0; ec++; } void Edmonds_Karp(int s, int t) { bool has_ap = false; do { has_ap = false; for (int i = 1; i < 10000; i++) { V[i].inq = false; } hp = tp = 0; enqueue(s); V[s].inq = true; while (hp != tp) { int u = dequeue(); if (u == t) { int x = u, apflow = INF; while (x != s) { if (E[V[x].pree].flow < apflow) { apflow = E[V[x].pree].flow; } x = V[x].prev; } ans += apflow; x = u; while (x != s) { E[V[x].pree].flow -= apflow; E[V[x].pree ^ 1].flow += apflow; x = V[x].prev; } has_ap = true; break; } for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].inq == false && E[cur].flow > 0) { enqueue(E[cur].endp); V[E[cur].endp].inq = true; V[E[cur].endp].prev = u; V[E[cur].endp].pree = cur; } } } } while (has_ap == true); } int main() { int k, u; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &k); for (int j = 0; j < k; j++) { scanf("%d", &u); add_edge(i, u + 1000); } } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &u); add_edge(u + 1000, 9999); } for (int i = 1; i <= n; i++) { add_edge(9998, i); } Edmonds_Karp(9998, 9999); printf("%d", ans); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define TRIE_SIZE 100000 using namespace std; struct node { int cnt; int next[26]; }T[TRIE_SIZE]; int nc = 2; void insert(char *s) { int len = strlen(s), cur = 1; for (int i = 0; i < len; i++) { if (T[cur].next[s[i]] == 0) { T[cur].next[s[i]] = nc; cur = nc; nc++; } else { cur = T[cur].next[s[i]]; } } T[cur].cnt++; } int query(char *s) { int len = strlen(s), cur = 1; for (int i = 0; i < len; i++) { if (T[cur].next[s[i]] == 0) { return 0; } else { cur = T[cur].next[s[i]]; } } return T[cur].cnt; } int main() { int o; char s[100]; while (true) { scanf("%d%s", &o, s); if (o == 1) { insert(s); } else if (o == 2) { printf("%d\n", query(s)); } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <algorithm> #define VERTEX_COUNT 100000 #define EDGE_COUNT 100000 typedef long long ll; using namespace std; struct edge { int u, v, w; }E[EDGE_COUNT]; int ivc, iec; int fa[VERTEX_COUNT], size[VERTEX_COUNT]; bool E_cmp(edge x, edge y) { return x.w < y.w; } void init_dsu() { for (int i = 1; i <= ivc; i++) { fa[i] = i; size[i] = 1; } } int getrt(int x) { if (fa[x] == x) { return x; } return (fa[x] = getrt(fa[x])); } void merge(int rtx, int rty) { if (size[rtx] < size[rty]) { fa[rtx] = rty; } else { fa[rty] = rtx; } } ll Kruskal() { int cnt = 1; ll wtotal = 0; for (int i = 0; i < iec && cnt < ivc; i++) { int rtu = getrt(E[i].u), rtv = getrt(E[i].v); if (rtu != rtv) { merge(rtu, rtv); cnt++; wtotal += E[i].w; } } return wtotal; } int main() { scanf("%d%d", &ivc, &iec); init_dsu(); for (int i = 0; i < iec; i++) { scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w); } sort(E, E + iec, E_cmp); ll res = Kruskal(); printf("%lld", res); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct vertex { int first, depth; }V[100010]; struct edge { int endp, next; }E[200010]; int ec = 1, ivc; int fa[100010][25]; void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } void DFS(int u, int prev, int depth) { if (fa[u][0] == 0) { fa[u][0] = prev; V[u].depth = depth; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { DFS(E[cur].endp, u, depth + 1); } } } void init() { DFS(1, -1, 1); for (int i = 1; i <= 20; i++) { for (int j = 1; j <= ivc; j++) { fa[j][i] = fa[fa[j][i - 1]][i - 1]; } } } int lca(int a, int b) { if (V[b].depth > V[a].depth) { int temp = a; a = b; b = temp; } int delta = V[a].depth - V[b].depth, k = 0; while (delta > 0) { if ((delta & 1) == 1) { a = fa[a][k]; } k++; delta >>= 1; } if (a == b) { return a; } for (int i = 20; i >= 0; i--) { if (fa[a][i] != fa[b][i]) { a = fa[a][i]; b = fa[b][i]; } } return fa[a][0]; } int main() { int u, v; scanf("%d", &ivc); for (int i = 1; i < ivc; i++) { scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } init(); while (true) { scanf("%d%d", &u, &v); printf("%d\n", lca(u, v)); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define VERTEX_COUNT 100000 #define EDGE_COUNT 100000 using namespace std; struct vertex { int first_edge; bool vis; }V[VERTEX_COUNT]; struct edge { int endp, next; }E[EDGE_COUNT]; int ec = 1; int Q[VERTEX_COUNT]; int hp = 0, tp = 0; void enqueue(int x) { Q[tp++] = x; } int dequeue() { return Q[hp++]; } bool queue_empty() { return hp == tp; } void add_edge(int u, int v) { E[ec].next = V[u].first_edge; V[u].first_edge = ec; E[ec].endp = v; ec++; } void BFS() { while (!queue_empty()) { int u = dequeue(); for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next) { if (V[E[cur].endp].vis == false) { V[E[cur].endp].vis = true; enqueue(E[cur].endp); } } } } int main() { int ivc, iec; scanf("%d%d", &ivc, &iec); for (int i = 0; i < iec; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); } enqueue(1); BFS(); return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> using namespace std; int k, s[100], sg[10001]; bool has[10001]; void init_sg() { sg[0] = 0; for (int i = 1; i <= 10000; i++) { memset(has, false, sizeof(has)); for (int j = 0; j < k; j++) { if (i - s[j] >= 0) { has[sg[i - s[j]]] = true; } } for (int j = 0; ; j++) { if (has[j] == false) { sg[i] = j; break; } } } } int main() { while (true) { scanf("%d", &k); if (k == 0) { break; } for (int i = 0; i < k; i++) { scanf("%d", &s[i]); } init_sg(); int t, c, x, res; scanf("%d", &t); while (t--) { res = 0; scanf("%d", &c); while (c--) { scanf("%d", &x); res ^= sg[x]; } if (res == 0) { printf("L"); } else { printf("W"); } } printf("\n"); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #define NIL 0 #define ALPHABET_SIZE 26 using namespace std; struct node { node *next[ALPHABET_SIZE], *parent; int len; node(int _len = 0) : len(_len) { memset(next, 0, sizeof(next)), parent = NIL; } }*root, *last; char s[100000]; int len; void init() { root = new node(0); last = root; } void extend(int x) { node *p = last; node *np = new node(p->len + 1); while (p != NIL && p->next[x] == NIL) { p->next[x] = np; p = p->parent; } if (p == NIL) { np->parent = root; } else { node *q = p->next[x]; if (q->len == p->len + 1) { np->parent = q; } else { node *nq = new node; *nq = *q; nq->len = p->len + 1; q->parent = np->parent = nq; while (p != NIL && p->next[x] == q) { p->next[x] = nq; p = p->parent; } } } last = np; } int main() { scanf("%s", s); len = strlen(s); init(); for (int i = 0; i < len; i++) { extend(s[i] - 'a'); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #include <cstring> #include <algorithm> #define NIL 0 using namespace std; struct node { node *next[26]; int cnt; node() : cnt(0) { memset(next, 0, sizeof(next)); } }; struct vertex { int first, depth; node *root; }V[100010]; struct edge { int endp, next; }E[200010]; int n, ec = 2, q, fa[100010][18], k; char str[100010][12]; inline void add_edge(int u, int v) { E[ec].next = V[u].first; V[u].first = ec; E[ec].endp = v; ec++; } void insert(node *&u, node *old, char *s, int len, int p) { u = new node; if (old != NIL) *u = *old; u->cnt++; if (p == len) return; int x = s[p] - 'a'; u->next[x] = NIL; insert(u->next[x], old == NIL ? NIL : old->next[x], s, len, p + 1); } int query(node *u, char *s, int len, int p) { if (u == NIL) return 0; if (p == len) return u->cnt; int x = s[p] - 'a'; return query(u->next[x], s, len, p + 1); } int query(int u, char *s) { return query(V[u].root, s, strlen(s), 0); } void dfs(int u, int fa, int depth) { ::fa[u][0] = fa; V[u].depth = depth; for (int cur = V[u].first; cur != 0; cur = E[cur].next) { if (E[cur].endp != fa) { insert(V[E[cur].endp].root, V[u].root, str[cur >> 1], strlen(str[cur >> 1]), 0); dfs(E[cur].endp, u, depth + 1); } } } void init() { V[1].root = new node; dfs(1, 0, 0); for (k = 1; (1 << k) <= n; k++) { for (int i = 1; i <= n; i++) { fa[i][k] = fa[fa[i][k - 1]][k - 1]; } } k--; } int lca(int x, int y) { if (V[x].depth < V[y].depth) { swap(x, y); } for (int i = k; i >= 0; i--) { if (V[x].depth - (1 << i) >= V[y].depth) { x = fa[x][i]; } } if (x == y) { return x; } for (int i = k; i >= 0; i--) { if (fa[x][i] != fa[y][i]) { x = fa[x][i]; y = fa[y][i]; } } return fa[x][0]; } int main() { scanf("%d", &n); int u, v; for (int i = 1; i < n; i++) { scanf("%d%d%s", &u, &v, str[i]); add_edge(u, v); add_edge(v, u); } init(); scanf("%d", &q); char s[12]; while (q--) { scanf("%d%d%s", &u, &v, s); printf("%d\n", query(u, s) + query(v, s) - query(lca(u, v), s) * 2); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MAX_ELEMENT_COUNT 5010 using namespace std; int fa[MAX_ELEMENT_COUNT], size[MAX_ELEMENT_COUNT]; int getrt(int v) { if (fa[v] == v) { return v; } return (fa[v] = getrt(fa[v])); } void merge(int x, int y) { int rx = getrt(x), ry = getrt(y); if (size[rx] < size[ry]) { fa[rx] = ry; size[ry] += size[rx]; } else { fa[ry] = rx; size[rx] += size[ry]; } } void initial(int n) { for (int i = 1; i <= n; i++) { fa[i] = i; size[i] = 1; } } int main() { // e.g. CodeVS - 1073. int n, m, p; scanf("%d%d%d", &n, &m, &p); initial(n); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); merge(a, b); } for (int i = 0; i < p; i++) { int a, b; scanf("%d%d", &a, &b); if (getrt(a) == getrt(b)) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define MAX_N 10000000 typedef long long ll; using namespace std; // p must be a prime greater than n. ll n, fac_inv[MAX_N], p; ll qpow(ll x, ll y) { if (y == 1) return x; ll t = qpow(x, y >> 1); t = t * t % p; if ((y & 1) == 0) return t; else return t * x % p; } ll fac(ll x) { ll res = 1; for (int i = 2; i <= x; i++) { res = res * i % p; } return res; } void fac_inv_sieve() { fac_inv[n] = qpow(fac(n), p - 2); for (ll i = n - 1; i >= 1; i--) { fac_inv[i] = fac_inv[i + 1] * (i + 1) % p; } } int main() { scanf("%lld%lld", &n, &p); fac_inv_sieve(); for (int i = 1; i <= n; i++) { printf("inv(fac(%d)) mod %lld = %lld\n", i, p, fac_inv[i]); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> using namespace std; struct complex { double re, im; complex(double _re = 0, double _im = 0) : re(_re), im(_im) { } }; complex operator + (const complex &x, const complex &y) { return complex(x.re + y.re, x.im + y.im); } complex operator - (const complex &x, const complex &y) { return complex(x.re - y.re, x.im - y.im); } complex operator * (const complex &x, const complex &y) { return complex(x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re); } complex operator / (const complex &x, const complex &y) { return complex((x.re * y.re + x.im * y.im) / (y.re * y.re + y.im * y.im), (x.im * y.re - x.re * y.im) / (y.re * y.re + y.im * y.im)); } int main() { complex x, y, res; char op; while (true) { scanf("%lf%lf %c %lf%lf", &x.re, &x.im, &op, &y.re, &y.im); switch (op) { case '+': res = x + y; break; case '-': res = x - y; break; case '*': res = x * y; break; case '/': res = x / y; break; default: continue; } printf("(%.2lf + (%.2lf)i) %c (%.2lf + (%.2lf)i) = %.2lf + (%.2lf)i\n", x.re, x.im, op, y.re, y.im, res.re, res.im); } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }
#include <cstdio> #define NIL 0 using namespace std; struct node { int key, value; node *ch[2]; node(int _key = 0, int _value = 0) : key(_key), value(_value) { ch[0] = ch[1] = NIL; } }*root; void rotate(node *&u, int dir) { node *o = u->ch[dir]; u->ch[dir] = o->ch[dir ^ 1]; o->ch[dir ^ 1] = u; u = o; } inline int compare(node *u, int key) { if (key == u->key) { return -1; } return key < u->key ? 0 : 1; } void insert(node *&u, int key, int value) { if (u == NIL) { u = new node(key, value); return; } int k0 = compare(u, key); if (k0 == -1) { return; } if (u->ch[k0] == NIL) { u->ch[k0] = new node(key, value); } else { int k1 = compare(u->ch[k0], key); if (k1 == -1) { return; } insert(u->ch[k0]->ch[k1], key, value); if (k0 == k1) { rotate(u, k0); } else { rotate(u->ch[k0], k1); } } rotate(u, k0); } int find(node *&u, int key) { if (u == NIL) { return -1; } int k0 = compare(u, key); if (k0 == -1) { return u->value; } if (u->ch[k0] == NIL) { return -1; } else { int k1 = compare(u->ch[k0], key), res; if (k1 == -1) { res = u->ch[k0]->value; goto END; } res = find(u->ch[k0]->ch[k1], key); if (u->ch[k0]->ch[k1] != NIL) { if (k0 == k1) { rotate(u, k0); } else { rotate(u->ch[k0], k1); } } END: rotate(u, k0); return res; } } int main() { int in, key, value; while (true) { scanf("%d", &in); if (in == 1) { scanf("%d%d", &key, &value); insert(root, key, value); } else if (in == 2) { scanf("%d", &key); printf("%d\n", find(root, key)); } else if (in == 0) { return 0; } else { printf("No such command!\n"); } } return 0; }
{ "repo_name": "Dev-XYS/Algorithms", "stars": "1116", "repo_language": "C++", "file_name": "Knuth-Morris-Pratt.cpp", "mime_type": "text/x-c++" }