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++)

[code].....

What is wrong with this code?

View 7 Replies


ADVERTISEMENT

C++ :: Implementation Of Longest Common Subsequence

Sep 11, 2014

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++)

[Code].....

View 2 Replies View Related

C++ :: Longest String Does'n Print On Same Line

Aug 4, 2014

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 << "

[code]....

View 3 Replies View Related

C++ :: Getting Longest Sequence In Hailstone Program

Oct 4, 2014

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)

[Code] ....

View 8 Replies View Related

C++ :: Program That Uses Recursion To Find Longest Increasing Sequence From A Grid In File

Mar 25, 2013

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;

[Code] .....

View 1 Replies View Related

C++ :: Reducing Code Duplication From Common Code Calling Common Class

Apr 13, 2014

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?

View 4 Replies View Related

C :: Find The Maximum Subsequence Sum

Mar 12, 2014

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?

View 8 Replies View Related

C :: Function To Detect Subsequence String

May 1, 2013

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.

View 11 Replies View Related

C++ :: Code That Returns Maximum Sum Of A Subsequence From A Array

Aug 25, 2014

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)

[Code]...

View 11 Replies View Related

C++ :: Find Longest Name / Display It And Tell How Many Letters It Have

Feb 27, 2014

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];

[Code] .....

View 5 Replies View Related

C++ :: Finding The Longest Path In A Graph

Dec 29, 2013

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.

View 2 Replies View Related

C/C++ :: Find Last Longest Word In A String

Apr 29, 2015

How to find the last longest word in a string when there are more of them (with the longest size)?

Here is the program for the longest without checking if last:

#include<stdio.h>
#include<string.h>
int main() {
char a[50],b[20],c[20];
int i,j=0,l=0;
printf("Enter a string:
");
gets(a);

[Code] ....

View 6 Replies View Related

Visual C++ :: Getting Longest Word From Array?

Feb 14, 2013

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>

[Code].....

View 1 Replies View Related

C :: How To Find Longest Series Of Even And Positive Numbers

Mar 6, 2015

Example input: 2 4 6 -1 7 3 -2 1

Output: 3 3 (longest series of even is 3, longest series of positive is 3)

Here is the code:

Code:

#include <stdio.h>
int even(int x) {
return x % 2 == 0;
}
int positive(int x) {
return x>0;

[Code]...

My question is how to write this code if the prototype of function is:

Code: void series(int *array, int n, int (*s)(int), int **begining, int *lenght);

View 1 Replies View Related

C/C++ :: Find Longest Series Of Even And Positive Numbers?

Mar 16, 2015

program:

Example input: 2 4 6 -1 7 3 -2 1
Output: 3 3 (longest series of even is 3, longest series of positive is 3)

Here is the code:

#include <stdio.h>
int even(int x) {
return x % 2 == 0;
}

[Code].....

My question is how to write this code if the prototype of function is:

void series(int *array, int n, int (*s)(int), int **beginning, int *length);

View 1 Replies View Related

C++ :: Given A String How To Find Longest Substring With All Unique Characters

Feb 9, 2015

I am new to C++ programming, writing a program for the below question:

given a string, how to find the longest substring with all unique characters.

View 1 Replies View Related

C/C++ :: Common Elements Of Two Arrays

Jan 23, 2015

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++) {

[Code] ....

View 14 Replies View Related

C++ :: How To Calculate Greatest Common Laboratory

Dec 9, 2013

#include<iostream>
using namespace std;
int GCD (int num , int x ) {
for ( int m = x ; m <= num ; m++ ) {

[Code] ....

View 1 Replies View Related

C++ :: Calculating Greatest Common Divisor?

Jan 12, 2013

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.

float gcd(float a, float b){
if (a>b) {
if(b==0){

[Code]....

View 1 Replies View Related

C++ ::  Finding Most Common Character Within A String

Oct 24, 2013

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.

View 8 Replies View Related

C++ :: Finding Greatest Common Divisor?

Jan 2, 2015

Is there a function or algorithm in stl in c++ the gcd of a vector ?

View 1 Replies View Related

C++ :: Greatest Common Divisor Of Two Numbers

Nov 19, 2014

How do I get the greatest common divisor of two numbers in C++?

View 1 Replies View Related

C/C++ :: Greatest Common Factor Of Many Numbers

Jun 15, 2014

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);
}

View 3 Replies View Related

C++ ::  can't Get Program To Print Only Available Seats

May 1, 2014

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;

[code].....

View 6 Replies View Related

C++ :: A Program To Print Big Letters?

Nov 28, 2013

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.

[URL]

View 6 Replies View Related

C++ :: Program To Print Sum Of Even And Odd Numbers

Apr 14, 2013

I am writing a program that prints the sum of even and odd numbers and here is my code

#include <iostream>
using namespace std;
const int SENTINEL = -999;

int main() {
int integers = 0;
int even = 0;
int odd = 0;

[Code] ....

Nut now in the output it adds -999 to the odd numbers ....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved