C++ :: Program To Print Longest Common Subsequence?
Sep 12, 2014
I found this implementation on a website for printing the longest common subsequence. But it gives wrong answers for some reason even though the code seems right to me.
Here is the code:
#include <iostream>
int lcs(char *X, char *Y, int m, int n)
{
int L[m+1][n+1];
for(int i = 0; i<=m; i++)
In the implementation of a program to find the length of the longest common subsequence, what does line 14 do?
void lcs( char *X, char *Y, int m, int n ) { int L[m+1][n+1]; /* Following steps build L[m+1][n+1] in bottom up fashion. Note that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */ for (int i=0; i<=m; i++)
I try to learn string class, resolving some problem, but i have some dificulties.The is ok, but when i print the longest string it does'n print on the same line.I enter the number of string, after that i enter the first string until i introduced from keyboard "#" character. I enter the second string and so on.Look at these example :
For i = 3;
Text[0] : I learn class String# Text[1] : I dont learn class String# Text[2] : String#
It print me like that : Text[1] :
I dont learn class String More than that look at the next example :
For i = 3;
Text[0] : I learn class String#abcdef Text[1] : I dont learn class String# Text[2] : String#
You see that in the first sentence i have continue to introduce some characters after # character and look what is happened :
Text[1] : abcdef I dont learn class String
#include<iostream> #include<string> using namespace std; int main() { string text[100], cuvant; int i, j, m, lung = 0; cout << "
The following is a part of a hailstone cpp program. I want to write an option to let users enter 2 numbers and the program evaluates the longest sequence the number has within the 2-number range and return the count.
Code: int longestHailstoneSequenceLength(int start, int end) { int max = 0; int s= start; while (s!=1)
Any examples of a c++ program that uses recursion to find the longest increasing sequence from a grid in a file. Like
2 4 6 8 10 12 14 16 18 20 22 24
I have to use a structure named Point and a structure named Sequence.
const int MAXROWS = 4; const int MAXCOLS = 4; const int MAXFILENAME = 255; // Structure used to define a point (x,y) in the grid. typedef struct { int x, y;
I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.
However, Is designing a complete class just for a few functions over kill?
Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?
I got a code written in Java. But, I gave up writing code in Java. The program written is supposed to find the maximum subsequence sum. It's originally like this.
Code: private static int maxSumRec (int [] a, int left, int right) { if(left == right) if(a[left > 0]) return arr[left];
[Code] .....
I turned it into C, add some elements (to generate random numbers and change some variables' names), and becomes like this
Code: int maxSumRec (val, left, right) { int x; long int arr[val]; srand ( time(NULL) ); for(x=0; x<val; x++)
[Code] .....
It fails to compile. What have I done wrong? And I keep wondering why in the original code there is left and right variables and their values are never assigned. My c compiler (I use codeblocks) keeps telling me that. Idk why. My friend who keeps it in Java says it is fine but he cannot explain how his program works. What *is* left and right actually?
So I need to write a function that has 2 arguments that are strings. It returns 1 if the first string is a subsequence of the second string, and 0 otherwise.
For example, given 2 strings like "foobar" and "obr", the function should returns 1 whereas "foobar" and "obo" returns 0.I have tried writing the code myself,
Code: int sub(char *s, char *t) { int i; char *p; for (i=0; t[i] != ''; i++) { p=strchr(s,t[i]); if (p==NULL) return 0; else strcpy(s,p); } return 1; }
So my strategy is as follows: 1. Find the first letter of the second string in the first string (foobar). 2. If there's a match then continue to search for the second letter of the second string starting from the index of the first letter plus 1. (obar). Otherwise return 0,.........
However the function always return 1 no matter what the input strings are.
Here's the code that returns maximum sum of a subsequence from a given array. This is according to "Programming Pearls" by Jon Bentley. I have come up with an example for which this program won't work. And here's that example:
{ -10, 11, 10, -10, 2, 3, -6, 1 }
Max subsequence sum above is 21. 2nd and 3rd elements.
But the program returns 16. It sums 2nd through 6th elements and returns. Why would the writer explain something with such depth, only to give a program that doesn't work in all instances?
int MaxSum(int lo, int hi, int* arr) { if (lo > hi) { return 0; } if (lo == hi)
I am trying to make a program into which you writre for example ten Names or words or etc. and it would find the longest name/word write it and the numbers of characters too but I have problem with displaying that name. Here is me source code, the problem is here "vitaz=szInput;" i don't know how to save that longest name/word.
#include <cstdlib> #include <iostream> #include <string.h> using namespace std; int main () { char a; char szInput[256],vitaz[256];
I was asked to find the longest path in a graph. I was thinking about using Dijsktra's algorithm after multiplying all the weights with -1 and run the program in normal way and find the shortest path. And then I'll multiply with -1 again and get the longest path. I think this should give me the longest path, do you think it would work? And also, I'm dealing with considerably big data, such as 1.000.000 nodes and many more edges. etc. and I have a time limit of 2 seconds and memory limit of 128mb. Any other data structure instead of Adjacency Matrix? Because I'm pretty sure it will exceed the limits.
I've noticed around here that using namespace std etc isn't exactly good practice but if I take a code back to my tutor without it I'll get sent back to change it and put it back in. I can't work out how to call the plant with the longest Latin name.
Code: // Session6_4.cpp : Defines the entry point for the console application. //For you to do... #include "stdafx.h" #include <iostream>
So I'm trying to make two arrays, then the third array that will include common elements of those two arrays. I did make a code for that, but the problem is I don't know how to include that the elements do not repeat.
For example, if there are two arrays,
Elements of the first one: 2, 3
Elements of the second one: 2, 3, 2
The third array is going to be: 2 2
While I want it to be only 2.
#include <iostream> using namespace std; int* intersection(int* n1, int d1, int* n2, int d2, int& d3) { d3=0; for (int i=0; i<d1; i++) {
I have written the following function to calculate GCD of floating point numbers, but when I run this for (111.6, 46.5), the calculation of fmod(a,b) in the funciton starts giving the wrong result after 2 recursive calls. I am unable to find the error here.
I am trying to take a string that is within the main function, and write a void function that gives me the most common alpha character used inside the string. How to mix a string and an array together like that as I am not too familiar with arrays yet.
How can I calculate GCF of many numbers? I thought I could calculate two by two numbers, but it not seems to be a very effective idea. There is my function:
int gcf (unsigned int x, unsigned int y) { return (y == 0) ? x : gcf (y, x % y); }
I can't get my program to print only AVAILABLE SEATS; instead it prints Both Available and Taken.
//Program allows you to delete and add seats to the Airplane. Also to upload seat settings as a text file. #include <iostream> #include <fstream> //file stream for text file #include <string> using namespace std;
I need help writing a program where I use small letter to write names in Big letters. (I dont know if you understand what i mean). Follow link to description.