From 1fa3702ad8123fce95d4c2fe1647e834a1ce4447 Mon Sep 17 00:00:00 2001 From: jaskirat1208 Date: Sat, 11 Feb 2017 07:37:12 +0530 Subject: [PATCH 1/2] DP problem --- algorithms/DP/sherlock_and_cost.cpp | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 algorithms/DP/sherlock_and_cost.cpp diff --git a/algorithms/DP/sherlock_and_cost.cpp b/algorithms/DP/sherlock_and_cost.cpp new file mode 100644 index 0000000..7de5014 --- /dev/null +++ b/algorithms/DP/sherlock_and_cost.cpp @@ -0,0 +1,52 @@ +nclude +#include +#include +#include +//#include +int max(int a,int b){ + if(a Date: Sun, 12 Feb 2017 21:49:34 +0530 Subject: [PATCH 2/2] some more files --- C++/Introduction/Variable_Sized_Array.cpp | 39 ++++++++++ C++/STL/Deque.cpp | 89 +++++++++++++++++++++++ C++/Strings/StringStream.cpp | 34 +++++++++ C++/Strings/attribut_parser.cpp | 75 +++++++++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 C++/Introduction/Variable_Sized_Array.cpp create mode 100644 C++/STL/Deque.cpp create mode 100644 C++/Strings/StringStream.cpp create mode 100644 C++/Strings/attribut_parser.cpp diff --git a/C++/Introduction/Variable_Sized_Array.cpp b/C++/Introduction/Variable_Sized_Array.cpp new file mode 100644 index 0000000..c43ddd0 --- /dev/null +++ b/C++/Introduction/Variable_Sized_Array.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main(int argc, char *argv[]) { + int n; + int q; + cin >> n >> q; + + // Create an array of pointers to integer arrays + // (i.e., an array of variable-length arrays) + int** outer = new int*[n]; + + // Fill each index of 'outer' with a variable-length array + for(int i = 0; i < n; i++) { + int k; + cin >> k; + // Create an array of length 'k' at index 'i' + outer[i] = new int[k]; + + // Fill each cell in the 'inner' variable-length array + for(int j = 0; j < k; j++) { + cin >> outer[i][j]; + } + } + + // Perform queries: + while(q-- > 0) { + int outer_index; + int inner_index; + cin >> outer_index >> inner_index; + + // Find the variable-length array located at outer_index + // and print the value of the element at inner_index. + cout << outer[outer_index][inner_index] << endl; + } + + return 0; +} diff --git a/C++/STL/Deque.cpp b/C++/STL/Deque.cpp new file mode 100644 index 0000000..b702a70 --- /dev/null +++ b/C++/STL/Deque.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; +/*void printKMax(int arr[], int n, int k){ + //Write your code here. + int max=0,i=0; + deque Q; + //constructing the queue + while(Q.size()!=k) + { + Q.push_front(arr[i]); + i++; + } + //iterating over the queue + while(i<=n){ + //maxima + max=0; + deque::iterator iter; + for(iter=Q.begin();iter!=Q.end();iter++){ + if(*iter>=max) max=*iter; + } + cout< Qi(k); + + /* Process first k (or first window) elements of array */ + int i; + for (i = 0; i < k; ++i) + { + // For very element, the previous smaller elements are useless so + // remove them from Qi + while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()]) + Qi.pop_back(); // Remove from rear + + // Add new element at rear of queue + Qi.push_back(i); + } + + // Process rest of the elements, i.e., from arr[k] to arr[n-1] + for ( ; i < n; ++i) + { + // The element at the front of the queue is the largest element of + // previous window, so print it + cout << arr[Qi.front()] << " "; + + // Remove the elements which are out of this window + while ( (!Qi.empty()) && Qi.front() <= i - k) + Qi.pop_front(); // Remove from front of queue + + // Remove all elements smaller than the currently + // being added element (remove useless elements) + while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()]) + Qi.pop_back(); + + // Add current element at the rear of Qi + Qi.push_back(i); + } + + // Print the maximum element of last window + cout << arr[Qi.front()]<> t; + while(t>0) { + int n,k; + cin >> n >> k; + int i; + int arr[n]; + for(i=0;i> arr[i]; + printKMax(arr, n, k); + t--; + } + return 0; +} + diff --git a/C++/Strings/StringStream.cpp b/C++/Strings/StringStream.cpp new file mode 100644 index 0000000..69a5917 --- /dev/null +++ b/C++/Strings/StringStream.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +using namespace std; + +vector parseInts(string str) { + // Complete this function + vector v1; + int i=0; + stringstream ss; + ss <>a>>ch; + v1.push_back(a); + while(ch==','){ + ss>>a>>ch; + v1.push_back(a); + //i++; + } + return v1; +} + +int main() { + string str; + cin >> str; + vector integers = parseInts(str); + for(int i = 0; i < integers.size(); i++) { + cout << integers[i] << "\n"; + } + + return 0; +} +1 diff --git a/C++/Strings/attribut_parser.cpp b/C++/Strings/attribut_parser.cpp new file mode 100644 index 0000000..6895ab6 --- /dev/null +++ b/C++/Strings/attribut_parser.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + /* Enter your code here. Read input from STDIN. Print output to STDOUT */ + int n,q; + char ch; + map mp; + scanf("%d %d\n",&n,&q); + string s,s1,s2,s3; + stringstream ss,ss1; + deque dq; + while(n--){ + getline(cin,s); + ss<>s1; + //cout<>s2>>ch>>s3; + if(ch=='-') break; + mp.insert(make_pair(ss1.str()+"~"+s2,s3)); + //cout<::iterator iter; + /*for(iter=mp.begin();iter!=mp.end();iter++){ + cout<first<>str2; + if(mp.find(str2)!=mp.end()){ + string t=mp.find(str2)->second; + cout<<(mp.find(str2)->second).substr(1,t.size()-2)<