Skip to content

Commit

Permalink
runtime input (#1381)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant Mahanta authored and GOVINDDIXIT committed May 24, 2019
1 parent 400397f commit 70b4e1b
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 80 deletions.
18 changes: 11 additions & 7 deletions Longest_Repeating_Subsequence/Longest_Repeating_Subsequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ int LongestRepeatingSubsequence(string str) {
length = str.length();

// Initialize all dp elements with 0
vector<vector<int>> dp(length+1, vector<int>(length+1, 0));
vector< vector<int> > dp(length + 1, vector<int>(length + 1, 0));

for (int i=1; i<=length; i++) {
for (int j=1; j<=length; j++) {
for (int i = 1; i <= length; i++) {
for (int j = 1; j <= length; j++) {

// If two characters are same(different position)
// then dp of present state is dp of previous state plus 1
if(i != j && str[i-1] == str[j-1])
dp[i][j] = dp[i-1][j-1] + 1;
if(i != j && str[i - 1] == str[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else {

// else maximum of just vertically above or horizontally left element
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
Expand All @@ -30,13 +30,17 @@ int LongestRepeatingSubsequence(string str) {

int main()
{
string str = "AlgoDsNoteDs";
string str;
cin >> str;
int length = LongestRepeatingSubsequence(str);
cout << "String: " << str << "\n";
cout << "Length of the longest repeating subsequence is: " << length << "\n";
}

/*
Sample Input:
AlgoDsNoteDs
Sample Output:
String: AlgoDsNotesDs
Length of longest repeating subsequence is: 3
Expand Down
20 changes: 13 additions & 7 deletions Longest_Repeating_Subsequence/Longest_Repeating_Subsequence.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import java.util.*;

public class Longest_Repeating_Subsequence {

static int LongestRepeatingSubsequence(String str) {
int length;
length = str.length();

// Intialize dp (default value is zero)
int dp[][] = new int[length+1][length+1];
int dp[][] = new int[length + 1][length + 1];

for (int i=1; i<=length; i++) {
for (int j=1; j<=length; j++) {
for (int i = 1; i <= length; i++) {
for (int j = 1; j <= length; j++) {

// If two characters are same(different position)
// then dp of present state is dp of previous state plus 1
if(i != j && str.charAt(i-1) == str.charAt(j-1))
dp[i][j] = dp[i-1][j-1] + 1;
if(i != j && str.charAt(i - 1) == str.charAt(j - 1))
dp[i][j] = dp[i - 1][j - 1] + 1;
else {

// Else maximum of just vertically above or horizontally left element
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
Expand All @@ -27,7 +29,8 @@ static int LongestRepeatingSubsequence(String str) {
}

public static void main(String args[]) {
String str = "AlgoDsNoteDs";
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int length = LongestRepeatingSubsequence(str);
System.out.println("String: " + str);
System.out.println("Length of longest repeating subsequence: " + length);
Expand All @@ -36,6 +39,9 @@ public static void main(String args[]) {
}

/*
Sample Input:
AlgoDsNoteDs
Sample Output:
String: AlgoDsNotes
Length of repeating subsequence: 3
Expand Down
18 changes: 10 additions & 8 deletions Longest_Repeating_Subsequence/Longest_Repeating_Subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ def LongestRepeatingSubsequence(string):
length = len(string)

# Initialize dp with zero
dp = [[0]*(length+1) for i in range(length+1)]
dp = [[0] * (length + 1) for i in range(length + 1)]

for i in range(1,length+1):
for j in range(1,length+1):
for i in range(1, length + 1):
for j in range(1, length + 1):

# If two characters are same(different position)
# then dp of present state is dp of previous state plus 1
if i != j and string[i-1] == string[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
if i != j and string[i - 1] == string[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:

# Else maximum of just vertically above or horizontally left element
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

return dp[length][length]

def main():
string = "AlgoDsNoteDs"
string = input()
length = LongestRepeatingSubsequence(string)
print("String: ", string)
print("Length of longest repeating subsequence: ", length)
Expand All @@ -29,7 +29,9 @@ def main():
main()

'''
Sample Input:
AlgoDsNoteDs
Sample Output:
String: AlgoDsNotes
Length of longest repeating subsequence: 3
Expand Down
69 changes: 39 additions & 30 deletions Square_Root_Decomposition/Square_Root_Decomposition.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
#include<bits/stdc++.h>
using namespace std;

void update(int index,int value,int *blocks,int input_arr[],int n){
blocks[(int)(index/sqrt(n))] += value-input_arr[index];
input_arr[index]=value;
void update(int index, int value, int *blocks, int input_arr[], int n){
blocks[(int)(index / sqrt(n))] += value - input_arr[index];
input_arr[index] = value;
}

//assuming 0 based indexing
int query(int low,int high,int *blocks,int input_arr[],int n){
int block_size=sqrt(n);
int sum=0;
int query(int low, int high, int *blocks, int input_arr[], int n){
int block_size = sqrt(n);
int sum = 0;

while(low % block_size!=0 && low<high){
sum+=input_arr[low];
while (low % block_size !=0 && low < high) {
sum += input_arr[low];
low++;
}
while(low+block_size<=high){
sum+=blocks[low/block_size];
low+=block_size;
while (low + block_size <= high) {
sum += blocks[low / block_size];
low += block_size;
}
while(low<=high){
sum+=input_arr[low];
while (low <= high) {
sum += input_arr[low];
low++;
}

return sum;

}

//divide the array in blocks of size sqrt(n), sum of elements in each block is stored in blocks
int *formBlocks(int input_arr[],int n){
int block_size=sqrt(n);
int index=-1;
int *blocks = new int[(int)(n/block_size)+1];
int *formBlocks(int input_arr[], int n){
int block_size = sqrt(n);
int index = -1;
int *blocks = new int[(int)(n / block_size) + 1];

for(int i=0;i<n;i++){
if(i%block_size==0){
for (int i = 0; i < n; i++) {
if (i % block_size == 0) {
index++;
}
blocks[index]+=input_arr[i];
blocks[index] += input_arr[i];
}
return blocks;
}
Expand All @@ -48,28 +47,38 @@ int main(){
for finding the sum of numbers in a given range
Indexing : 0 based in array
*/
//input : array of integers
int input_arr[]={2,3,4,6,8,9,1,-1,0,3}; //elements
int n = sizeof(input_arr)/sizeof(input_arr[0]); //number of elements
int *blocks=formBlocks(input_arr,n);

int n;
cin >> n;
int input_arr[n];
for (int i = 0; i < n; i++) {
cin >> input_arr[i];
}

int *blocks = formBlocks(input_arr,n);

//queries
//input queries with left and right indices to find the sum in that range
cout<<query(0,2,blocks,input_arr,n)<<endl; //find sum from index 0 to index 2
cout << query(0, 2, blocks, input_arr, n) << endl; //find sum from index 0 to index 2

cout<<query(3,7,blocks,input_arr,n)<<endl; //find sum from index 3 to index 7
cout << query(3, 7, blocks, input_arr, n) << endl; //find sum from index 3 to index 7

cout<<query(1,4,blocks,input_arr,n)<<endl; //find sum from index 1 to index 4
cout << query(1, 4, blocks, input_arr, n) << endl; //find sum from index 1 to index 4

//update
update(3,1,blocks,input_arr,n); //update the value at index 3 with value of 1
update(3, 1, blocks, input_arr, n); //update the value at index 3 with value of 1

//query
cout<<query(1,4,blocks,input_arr,n)<<endl; //find sum from index 1 to index 4
cout << query(1, 4, blocks, input_arr, n) << endl; //find sum from index 1 to index 4

return 0;
}

/*
Input:
10
2 3 4 6 8 9 1 -1 0 3
Expected output:
9
23
Expand Down
34 changes: 21 additions & 13 deletions Square_Root_Decomposition/Square_Root_Decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,33 @@ def formBlocks(input_arr):
for finding the sum of numbers in a given range
Indexing : 0 based in list
'''
input_arr=[2,3,4,6,8,9,1,-1,0,3]
blocks=formBlocks(input_arr)
#queries
'''
input queries with left and right indices to find the sum in that range
'''
print(query(0,2,blocks,input_arr)) #find sum from index 0 to index 2

print(query(3,7,blocks,input_arr)) #find sum from index 3 to index 7
def main():
input_arr = list(map(int, input().split()))
blocks = formBlocks(input_arr)
#queries
'''
input queries with left and right indices to find the sum in that range
'''
print(query(0, 2, blocks, input_arr)) #find sum from index 0 to index 2

print(query(3, 7, blocks, input_arr)) #find sum from index 3 to index 7

print(query(1,4,blocks,input_arr)) #find sum from index 1 to index 4
print(query(1, 4, blocks, input_arr)) #find sum from index 1 to index 4

#update
update(3,1,blocks,input_arr) #update the value at index 3 with value of 1
#update
update(3, 1, blocks, input_arr) #update the value at index 3 with value of 1

#verify if the update was success
print(query(1,4,blocks,input_arr))
#verify if the update was success
print(query(1, 4, blocks, input_arr))

if __name__ == '__main__':
main()

'''
Input:
2 3 4 6 8 9 1 -1 0 3
Expected output:
9
23
Expand Down
6 changes: 5 additions & 1 deletion Z_Algorithm/Z_Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ int main()
return 0;
}

/* Output
/*
Input:
namanchamanbomanamansanam
aman
Output:
Pattern found at 2
Pattern found at 8
Expand Down
20 changes: 10 additions & 10 deletions Z_Algorithm/Z_Algorithm.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.*;

public class Z_Algorithm {

Expand Down Expand Up @@ -37,31 +38,30 @@ static void getZarr(String str, int Z[]){
}
}

static void search(String text, String pattern){
static void search(String text, String pattern) {

String concat = pattern + "$" + text;
int size = concat.length();
int Z[] = new int[size];

getZarr(concat, Z);

for(int i = 0; i < size; i++){
if(Z[i] == pattern.length()){
int index = i-pattern.length();
for (int i = 0; i < size; i++) {
if (Z[i] == pattern.length()) {
int index = i - pattern.length();
System.out.println("Pattern found at " + index);
}
}


}

public static void main(String args[]){

String text = "namanchamanbomanamansanam";
String pattern = "aman";
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String pattern = sc.nextLine();
search(text, pattern);
}
}

/*
Sample Input:
namanchamanbomanamansanam (text)
Expand Down
16 changes: 12 additions & 4 deletions Z_Algorithm/Z_Algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ def search(text, pattern):
if Z[i] == len(pattern):
print("Pattern found at " + str(i - len(pattern)))

text = "namanchamanbomanamansanam"
pattern = "aman"
def main():
text = input()
pattern = input()

search(text, pattern)
search(text, pattern)

''' Output
if __name__ == '__main__':
main()

'''
Input:
namanchamanbomanamansanam
aman
Output:
Pattern found at 2
Pattern found at 8
Pattern found at 17
Expand Down

0 comments on commit 70b4e1b

Please sign in to comment.