Posts

Showing posts from October, 2016

uva 10976 - Fractions Again?! Solution

uva 10976 - Fractions Again?! #include<iostream> #include<algorithm> #include<vector> using namespace std; int main(){ unsigned int i, n,m,count; while (cin >> n){ vector<int>vec; m = 2 * n; count = 0; for (i = n+1; i <= m; i++){ if ((n*i)%(i-n) == 0){ count++; vec.push_back((n*i) / (i - n)); vec.push_back(i); } } cout << count << endl; i = 0; while (count--){ cout << "1/" << n << " = 1/" << vec[i++]; cout << " + 1/" << vec[i++] << endl; } } return 0; }

10667 - Largest Block Solution by using Histogram Area Algorithm

10667 - Largest Block  #include<iostream> #include<algorithm> #include<string> #include<stack> using namespace std; bool matrix[100][100]; int calculate_max_histogram_area(int *arr,int len){ stack<int> st; int i, max, temp,temp_sum,cur_index,sum; temp = 0; max = 0; temp_sum = 0; for (i = 0; i< len; i++){ if (arr[i]>0 && arr[i] >= temp){ temp = arr[i]; st.push(i); } else{ temp = arr[i]; while (true){ if (st.empty()) break; cur_index = st.top(); if (arr[cur_index] == temp) break; st.pop(); sum = arr[cur_index] * (i - cur_index); if (sum > max){ max = sum; } } if (i < len && arr[i]>0) st.push(i); } } while (!st.empty()){ cur_index = st.top(); st.pop(); sum = arr[cur_index] * (i - cur_index); if (sum > max){ max = sum; } } return max; } int calculate_max_sub_matrix(int n){ ...

uva- 836 - Largest Submatrix Solution

uva- 836 - Largest Submatrix   #include<iostream> #include<algorithm> #include<string> #include<stack> using namespace std; int calculate_max_histogram_area(int *arr,int len){ stack<int> st; int i, max, temp,temp_sum,cur_index,sum; temp = 0; max = 0; temp_sum = 0; for (i = 0; i< len; i++){ if (arr[i]>0 && arr[i] >= temp){ temp = arr[i]; st.push(i); } else{ temp = arr[i]; while (true){ if (st.empty()) break; cur_index = st.top(); if (arr[cur_index] == temp) break; st.pop(); sum = arr[cur_index] * (i - cur_index); if (sum > max){ max = sum; } } if (i < len && arr[i]>0) st.push(i); } } while (!st.empty()){ cur_index = st.top(); st.pop(); sum = arr[cur_index] * (i - cur_index); if (sum > max){ max = sum; } } return max; } int calculate_max_sub_matrix(string *input, int n){ ...

uva 108 - Maximum Sum - Solution using Kadane Algorithm

uva 108 - Maximum Sum - Solution using Kadane Algorithm #include<iostream> #include<algorithm> using namespace std; void kadane_algo(int *arr, int len, int &max_value){ if (len == 0) return; int tempSum  = arr[0]; if (tempSum > max_value) max_value = tempSum; for (int i = 1; i < len; i++){ if (tempSum < 0){ if (max_value <= arr[i]){ tempSum = max_value = arr[i]; } else{ tempSum += arr[i]; if (tempSum < arr[i]) tempSum = arr[i]; } } else{ tempSum += arr[i]; if (tempSum < arr[i]) tempSum = arr[i]; if (tempSum > max_value){ max_value = tempSum; } } } } void calculate_max_2d_rect_sum(int arr[][100], int row,int &max_value){ int temp[100], i, j, k, temp_max_value; max_value=temp_max_value = arr[0][0]; for (i = 0; i < row; i++){ fill(temp, temp + row, 0); for (j = i; j < row; j++){ for (k = 0; k < row; k++){ temp[k]...

uva 11713 - Abstract Names Solution

11713 - Abstract Names -Solution #include<iostream> #include<string.h> using namespace std; int main(){ int test,len1,len2,i; char str1[25], str2[25],ch1,ch2; cin >> test; bool flag; while (test--){ cin >> str1 >> str2; len1 = strlen(str1); len2 = strlen(str2); flag = true; if (len1 != len2) flag = false; else{ for (i = 0; i < len1; i++){ ch1 = str1[i]; ch2 = str2[i]; if (ch1 != ch2){ if (!((ch1 == 'a' || ch1 == 'e' || ch1 == 'i' || ch1 == 'o' || ch1 == 'u') && (ch2 == 'a' || ch2 == 'e' || ch2 == 'i' || ch2 == 'o' || ch2 == 'u'))){ flag = false; break; } } } } if (flag) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }