Faster edit distance algorithm [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Problem: I know the trivial edit distance DP formulation and computation in O(mn) for 2 strings of size n and m respectively. But I recently came to know that if we only need to calculate the minimum value of edit distance f and it is bounded |f|<=s, then we can calculate it in O(min(m,n) + s^2) or O(s*min(m,n)) [wikipedia] time.
Please explain the dp formulation behind it if this is DP based or explain the algorithm .
Look at the improved algorithm section of the
link: http://en.wikipedia.org/wiki/Edit_distance .
one more link about improved UKKONEN'S algorithm http://www.berghel.net/publications/asm/asm.php
Thanks in advance.

You can calculate edit distance in O(min(n, m) * s) time use next simple idea:
Consider the i-th string in DP-table.
So, if we know, that answer <= s, then we are intersted in cells with coordinates (i, i - s), (i, i - s + 1), ... ,(i, i + s). Because in other cells answer strictly greater than s.
For example, suppose we know, that edit distance between "abacaba" and "baadba" less than 3.
So, we can skip red cells, because they have value more than s.
Asymptotic of the algorithm O(min(n, m) * s) because we calculate s cells to the left and right of the main diagonal.

Related

Suppose you have an array of N elements. You need to find for how many i, Ai + A(i+1) is a square number. Is this question trivial? If so how? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I just wanted to know if the above question is trivial or not. More importantly, how can you recognize if an algorithm is trivial?
It depends what do you mean by trivial. If you talk about complexity, it is O(n*M(N)) where M(N) is the complexity of the underlying multiplication algorithm with N maximum of the array's values and n is the length of the array.
If you talk about implementation, it is one loop with one check that the sum of the neighbors is a perfect square. If the elements fit into int, double etc. you have sqrt function in the standard library. If your elements are arbitrary length integers or float point numbers, you either need to use an appropriate library or implement the handling of these numbers on your own, which might be not trivial.
This understanding should help you to answer your last question

Solving system of equation modulo 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I make a program in C++, that factorizes natural numbers. The only problem is to create a function that does the following:
input: it receives a matrix vector< vector< int> > M.
output: it gives a vector v so that a result of multiplying v and M is a vector that all its coordinates are equal to 0.
Everything must be modulo 2, so coefficients of M and v consists only 0s and 1s
Schould I use a Gauss elimination method? If so, how do this? The problem that implementations I saw on the net don't use vectors and the vectors are necessary in my main program
I would be grateful if someone helped me.
Regards
This is an interesting problem. The steps to be taken can be found in this exercise. link.
The tricky part is to understand that there are always only a finite number of solutions, i.e, only the trivial solution exists or non-trivial solutions exist.
Once you finish the row reduction steps and if non-trivial solutions exist, there is always going to be at least one independent variable (it can take any value 0/1) and the rest of the variables depend on the independent variables.

Confusion over how to code a for loop including a for all addition in c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am developing the optimization of a bus schedule problem in c++ and need to code each of the constraints, however I am struggling to know how to code a particular kind of constraint.
The sets i,j,k and h all range from 1 to I,J,K and H respectively. My confusion lies with how to code the left hand side of the constraint. I am trying to use for loops however I am getting confused as to how to implement the for all i,h part.
My question is how to code the left hand side of the equation, given that X is a four dimensional array. I so far have two for loops, looping from j,k = 0 to j,k < J,K. How would I include for all i,h
Any help would be greatly appreciated =)
In pseudo code that would be
for all i
for all h
sum = 0
for all j
for all k
sum += X[i,h,j,k]
if (sum != 1) -> condition not satisfied
You basically have for each combination of i and h a seperate condition that has to be satisfied. For each of those conditions you have to sum over j and k.

How to get common factors with c++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble starting with the code i have already read some other questions here but im still stuck on how am i even gonna start this one :/.
So i made 3 inputs to fill the expression (ax^2+bx+c)
.......
cout<<"This Program runs onlyy the expression (ax^2+bx+c)"<<endl;
cout<<"\nEnter The first Integer[a]";
cin>>ina;
cout<<"Enter the second Integer[b]";
cin>>inb;
cout<<"Enter The third Integer[c]";
cin>>inc;
cout<<"Your Expression is"<<endl;
cout<<ina<<"x^2 + "<<inb<<"x + "<<inc<<endl;
........
Now how would i make my program show its common factor..
example is (x^2+4x+3) how can i make it show that its common factor is (x+3) and (x+1)?
Well how to factor a polynomial has very little to do with C++.
If Ax^2 + Bx + C can be expressed as A*(x - x1)*(x - x2) (and it can always be so expressed), then clearly plugging the value x1 in for x makes the original equation zero, since the first term of (x1 - x1)*(x1 - x2) is then 0. And ditto for x2. And conversely, if you have two values that make the equation zero, then they are x1 and x2.
There is a standard formula for solving quadratic equations. Computing that in a program one should be aware that subtracting a number from a roughly equal size number can produce a less precise result. So how that formula is expressed in the code, can make a difference wrt. to the accuracy of the results. You can find more information about that on the net. Including examples for quadratic equation formula.

A computing trick to calculate for e.g number of boxes required to place N objects given each box can hold M objects? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
As a part of a recent topcoder SRM problem we had to compute number of buses "B" required to carry "N" people given that each bus has "S" seats.
What is the smartest way to compute this in C++?
The obvious way is to do:
if(N%S==0){B=N/S;}
else{ B=N/S + 1;}
^ ALL VARIABLES ARE INTEGERS, N AND S ASSIGNED APPROPRIATE VALUES
However I cant understand the logic behind the following code which is one particular topcoder user's solution which I was checking out;
B = (N + (S-1))/S;
How does this work?
The code
B = (N + (S-1))/S;
is a common rounding trick. We know that in integer division, the remainder is cut-off, essentially what floor does. In this case, we enforce a ceil operation by adding S-1 first.
This is similiar to the common way of rounding floating point numbers:
n = floor(n + 0.5);