LCS #include <iostream> #include <string> using namespace std; int lcs(string X, string Y, int m, int n) { if (m == 0 || n == 0) { return 0; } if (X[m - 1] == Y[n - 1]) { return 1 + lcs(X, Y, m - 1, n - 1); } else { return max(lcs(X, Y, m, n - 1), lcs(X, Y, m - 1, n)); } } int main() { string X, Y; getline(cin, X); // Get the first string from the user getline(cin, Y); // Get the second string from the user int m = X.length(); int n = Y.length(); int len = lcs(X, Y, m, n); // Computing the longest common subsequence string lcs_str(len, ' '); int i = m, j = n; while (i > 0 && j > 0) { if (X[i - 1] == Y[j - 1]) { lcs_str[--len] = X[i - 1]; i--; j--; } else if (lcs(X, Y, i - 1, j) > lcs(X, Y, i, j - 1)) { i--; } else { j--; } } cout << lcs_str << endl; return 0; } FLOYD WARSHALL #include <iostream> using namespace std; #define V 4 // number of vertices #define INF 100 // infinity void floydWarshall(int graph[][V]) { int dist[V][V]; for(int i=0; i<V; i++) for(int j=0; j<V; j++) dist[i][j] = graph[i][j]; for(int k=0; k<V; k++) for(int i=0; i<V; i++) for(int j=0; j<V; j++) if(dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; // Print the final cost matrix for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][j] == INF){ cout << INF ; if (j == V-1){ break; } cout<< " "; } else{ cout << dist[i][j] ; if (j == V-1){ break; } cout<< " "; } } cout << endl; } } int main() { int graph[V][V]; // Take input of cost matrix for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { cin >> graph[i][j]; } } floydWarshall(graph); return 0; } RABIN KARP 1 #include <iostream> #include <string> #include <cmath> using namespace std; #define prime 101 #define max_size 15 int calculateHash(string str, int start, int end); bool checkEqual(string str1, string str2, int start1, int end1, int start2, int end2){ if(end1-start1 != end2-start2){ return false; } for(int i=start1, j=start2; i<=end1 && j<=end2; i++, j++){ if(str1[i] != str2[j]){ return false; } } return true; } int calculateHash(string str, int start, int end){ int hash = 0; for(int i=start; i<=end; i++){ hash += str[i] * pow(prime, i-start); } return hash; } int main() { string main_str, pattern; cin >> main_str >> pattern; int main_hash = calculateHash(main_str, 0, pattern.length()-1); int pattern_hash = calculateHash(pattern, 0, pattern.length()-1); for(int i=0; i<=max_size-pattern.length(); i++) { if(main_hash == pattern_hash && checkEqual(main_str, pattern, i, i+pattern.length()-1, 0, pattern.length()-1)) { cout << pattern << endl; cout << i+1 << " " << i+pattern.length() << endl; return 0; } if(i < max_size-pattern.length()) { main_hash = (prime*(main_hash - main_str[i]*pow(prime, 0))) + main_str[i+pattern.length()]*pow(prime, pattern.length()-1); } } cout << "Not Matching" << endl; } RABIN KARP 2 #include <string.h> #include <iostream> using namespace std; int check = 0; #define d 10 void rabinKarp(string pattern, string text, int q) { int m = pattern.length(); int n = text.length(); int i, j; int p = 0; int t = 0; int h = 1; for (i = 0; i < m - 1; i++) h = (h * d) % q; for (i = 0; i < m; i++) { p = (d * p + pattern[i]) % q; t = (d * t + text[i]) % q; } for (i = 0; i <= n - m; i++) { if (p == t) { for (j = 0; j < m; j++) { if (text[i + j] != pattern[j]) break; } if (j == m){ check = 1; //cout << "Pattern is found at position: " << i + 1 << endl; cout << pattern << endl; cout << i+1 << " " << i+m+1<<endl;} } if (i < n - m) { t = (d * (t - text[i] * h) + text[i + m]) % q; if (t < 0) t = (t + q); } } if(check == 0){ cout << "Pattern not found"; } } int main() { string text,pattern; getline(cin,text); getline(cin,pattern); int q = 13; rabinKarp(pattern, text, q); }