Longest common substring

Longest common substring of two entered words

Page 1 of 1

12 Replies - 21271 Views - Last Post: 13 September 2012 - 11:06 AM Rate Topic: -----

#1 chghrtfzzy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 05-July 08

Longest common substring

Post icon  Posted 05 July 2008 - 10:49 PM

Hi. I'm totally new to programming and these boards, and I'm having a lot of trouble with this project.
I am supposed to write a C++ program that finds the longest common substring of two entered words. The program repeatedly prompts the user for two words and prints the longest substring that the two words have in common to the screen. The program quits when the user closes the input stream. I know we use for loops and nested loops . . . but that's all I know :/ I still don't quite get the "for loop" thing. Can someone explain that? The prof also said something about closing the program after the user enters cntrl+z twice? How do I do that? I'm not sure what that means.

Please help. I'm so lost :(
Thanks for any help.

This post has been edited by chghrtfzzy: 05 July 2008 - 11:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Longest common substring

#2 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Longest common substring

Posted 06 July 2008 - 11:39 AM

This is a famous problem and can be solved using dynamic programming algorithm, just Google and you will find a lot of good materials.

This post has been edited by lordms12: 06 July 2008 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Longest common substring

Posted 06 July 2008 - 11:53 AM

a "for loop" is a rather simple construct that is most often used to execute loops with a known number of iterations -- i.e. a loop that counts its way though a series of numbers (though this is by no means the only use if the for loop).

int x; // a counter variable
std::cout << "I can count to ten." << std::endl;
for (x=0; x <10; x++) {
	std::cout << x << ", ";
}
std::cout << "\nDone!\nNow Gimme Some CANDY!" << std::endl;


The loop has four parts:

for(initialization; condition needed to continue; increment) {
..inside of loop...
}

in the 'initialization' part you set things up, like set your loop variable to 0.

in the 'condition needed to continue' you set the condition needed for the inner portion of the loop to execute -- x < 10

in the increment step you modify the control variable. x++
Was This Post Helpful? 0
  • +
  • -

#4 chghrtfzzy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 05-July 08

Re: Longest common substring

Posted 06 July 2008 - 02:33 PM

View Postlordms12, on 6 Jul, 2008 - 11:39 AM, said:

This is a famous problem and can be solved using dynamic programming algorithm, just Google and you will find a lot of good materials.

Hi.
I've Googled it. The search comes with a lot of "longest common subsequence" results. I'm not sure what a subsequence but I think I've read somewhere that it isn't the same thing as a substring? And the other results used some stuff like Python (?) and C# (?), etc :crazy: So far we've only covered iostream, string, int, for loops, while loops, if-else statements, and do-while loops. We're only supposed to use those.


View PostNickDMax, on 6 Jul, 2008 - 11:53 AM, said:

a "for loop" is a rather simple construct that is most often used to execute loops with a known number of iterations -- i.e. a loop that counts its way though a series of numbers (though this is by no means the only use if the for loop).

int x; // a counter variable
std::cout << "I can count to ten." << std::endl;
for (x=0; x <10; x++) {
	std::cout << x << ", ";
}
std::cout << "\nDone!\nNow Gimme Some CANDY!" << std::endl;


The loop has four parts:

for(initialization; condition needed to continue; increment) {
..inside of loop...
}

in the 'initialization' part you set things up, like set your loop variable to 0.

in the 'condition needed to continue' you set the condition needed for the inner portion of the loop to execute -- x < 10

in the increment step you modify the control variable. x++

Thank you :) So its just a counting thing? So can it be used to compare strings-- letter by letter? :blink:

This post has been edited by chghrtfzzy: 06 July 2008 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Longest common substring

Posted 06 July 2008 - 03:45 PM

Well yes... mostly you want to put logic like that into the inside of the loop (though often you will see some programmers pack all kinds of crap into the other three section -- I believe that you program could be done in such a way, but I don't recommend it).

There are may ways to find the longest substring of a string. You should probably start with something simple and work your way into more and more sophisticated methods.

IT helps to start by trying to do this yourself. Write out two words and try to think of a step by step way to find common substrings.

so given Antidisestablishmentarianism and establishment.

Think of all the ways that you can find common substrings. Once you work out a method -- try to formalize it into an algorithm. THEN try to code it.
Was This Post Helpful? 0
  • +
  • -

#6 chghrtfzzy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 05-July 08

Re: Longest common substring

Posted 07 July 2008 - 12:00 AM

A friend told me that while loops would be more useful in this case since for loops aren't conventional . . . is this true?
Was This Post Helpful? 0
  • +
  • -

#7 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Longest common substring

Posted 08 July 2008 - 05:33 AM

i tried coding this problem but am unable to!
can any body guide me how to go about it?? its a really nice question
Was This Post Helpful? 0
  • +
  • -

#8 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 12
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Longest common substring

Posted 08 July 2008 - 05:37 AM

The best way to understand the for loop, or loops in general is to see how each can be interchanged to make inheretenly the same type of loop. How is your understanding on while loops, do while loops, etc.
Was This Post Helpful? 0
  • +
  • -

#9 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Longest common substring

Posted 08 July 2008 - 06:13 AM

am ok with loops i think
i just dont get the concept of this problem
i put two strings in two arrays then i thought i can compare the first position of the array1 with all the characters of array 2
if it matches then i thought i would move on to the next charcter of array1 and would like to continue from that position of array2

i tried coding but its getting really complicated!! i dont think am making sense
pls advice so that i can try again
Was This Post Helpful? 0
  • +
  • -

#10 chghrtfzzy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 05-July 08

Re: Longest common substring

Posted 08 July 2008 - 11:17 AM

#include <iostream>
#include <string>

using namespace std;
int main () {
	
	string first, second, lcsub, max;
	
	cout << "Enter two words" << endl;
	cin >> first >> second;
		for (int i=0; i < first.length(); i++)
			for (int j=0; j < second.length(); j++)
				for (int k=1; k <= first.length() && k <= second.length(); k++){
					if (first.substr(i,k) == second.substr(j,k)){
						lcsub = first.substr(i,k);
					}
					else{
						if (lcsub.length() > max.length())
							max=lcsub;
						lcsub="";
					}
				}
					if (lcsub.length() > max.length())
							max=lcsub;
						lcsub="";
						cout << "Longest Common Substring: " << max << endl;
	return 0;
}


Okay, so I got the longest common substring . . . now I just need to figure out how to repeatedly prompt the user to enter two words and close the input stream using ctrl+z :/

This post has been edited by chghrtfzzy: 08 July 2008 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 chghrtfzzy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 05-July 08

Re: Longest common substring

Posted 09 July 2008 - 01:52 PM

Got it! Still doesn't recognize capitalized letters (but that wasn't part of the requirements) :)

#include <iostream>
#include <string>

using namespace std;
int main () {
	
	while(1) {
		
		string first, second, lcsub, max;
		
		cout << "Enter two words" << endl;
		cin >> first >> second;
		if(cin.eof()) {
			return 0;
		}
		for (int i=0; i < first.length(); i++){
				for (int j=0; j < second.length(); j++){
					for (int k=1; k <= first.length() && k <= second.length(); k++){
						if (first.substr(i,k) == second.substr(j,k)){
							lcsub = first.substr(i,k);
						}
						else{
							if (lcsub.length() > max.length())
								max=lcsub;
							lcsub="";
						}
					}
						if (lcsub.length() > max.length())
								max=lcsub;
						lcsub="";
				}
		}
		cout << "Longest Common Substring: " << max << endl << endl;
	}
	return 0;
}

Was This Post Helpful? 1

#12 from_ashes  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 12-September 12

Re: Longest common substring

Posted 13 September 2012 - 10:13 AM

since u r a starter i suggest u to follow the naive algorithm which is based on brute force...and after going clean with that u can follow dp(dynamic programming ) for optimized results.
Was This Post Helpful? -1
  • +
  • -

#13 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,509
  • Joined: 23-August 08

Re: Longest common substring

Posted 13 September 2012 - 11:06 AM

Don't go dredging up 4 year old topics, please.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1