Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 6:38 PM   #1
jobobshishkabob
Programmer
 
Join Date: Jan 2006
Location: Texas
Posts: 36
Rep Power: 0 jobobshishkabob is on a distinguished road
Finding Patterns

Ok... please don't flame me for asking this again, but i didnt ask it properly last time and i did not word it correctly...

Is there any way in C++ to go through 2 strings of characters, for example 141337545434544543 and 325452134544534513375, and find similar patterns (look at the bold)... take all those similar patterns, and display them on a seperate page??? These patterns must be ATLEAST 5 characters each. It does not matter where they are in the strings... just as long as those patterns are the same.

[EXAMPLE]
(Pg 1)
Pattern 1:
141337545434544543
Pattern 2:
325452134544534513375

(Pg 2)
Following occurs in both patterns:
13375
45445
[/EXAMPLE]

Please, if you can, explain me a code that i can use to do this. THANK YOU IN ADVANCE!
jobobshishkabob is offline   Reply With Quote
Old Feb 16th, 2006, 7:37 AM   #2
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
If the strings are short, you can try to brute force it like:
1) break the first string in n(n is the pattern length) char long strings.
eg: if string==123456789, n==5
you break the string in 5 string viz.12345, 23456,34567,45678,56789
2) break the second string similarly.

3) now you have two lists containing patterns. you can compare each to each other and find the matches.

This is a very inefficient algorithm. But it is the first thing that came to my mind.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Feb 16th, 2006, 5:40 PM   #3
jobobshishkabob
Programmer
 
Join Date: Jan 2006
Location: Texas
Posts: 36
Rep Power: 0 jobobshishkabob is on a distinguished road
Quote:
Originally Posted by InfoGeek
If the strings are short, you can try to brute force it like:
1) break the first string in n(n is the pattern length) char long strings.
eg: if string==123456789, n==5
you break the string in 5 string viz.12345, 23456,34567,45678,56789
2) break the second string similarly.

3) now you have two lists containing patterns. you can compare each to each other and find the matches.

This is a very inefficient algorithm. But it is the first thing that came to my mind.
wow! thats almost exactly what i need... sorry im a HUGE noob at C++... can u give me a code? i would really appreciate that

only thing is my string is 10kb in notepad... kinda long... hopefully it will still work...

Thanks
jobobshishkabob is offline   Reply With Quote
Old Feb 16th, 2006, 5:46 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
At least five is not the same thing as exactly five. You need to start with the first character of the first string, use a 5-character needle, and search every position in the second string, then change needle size and do it all again. There are matching algorithms that can shorten this process below the number of comparisons required for the raw brute force approach, but I don't happen to have a link handly. Google would probably work.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 16th, 2006, 5:56 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
I think you mean change need location, rather than size.

Once you have found a 5 character match, it is pretty easy to then check how big the actual match is, as you now the location of the start of both matching patterns.
The Dark is offline   Reply With Quote
Old Feb 16th, 2006, 6:23 PM   #6
jobobshishkabob
Programmer
 
Join Date: Jan 2006
Location: Texas
Posts: 36
Rep Power: 0 jobobshishkabob is on a distinguished road
Google isnt being nice to me, sorry...

exactly 5 will work, i can just give the user an option and make many pages, replacing the 5 with anything...

thanks for your suggestion... but can someome give me some code? im a very big newbie in C++. thank you.
jobobshishkabob is offline   Reply With Quote
Old Feb 16th, 2006, 6:27 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
I think you mean change need location, rather than size.
If the size isn't fixed at 5, you need to up the size. After you've exhausted all those, then you change the location (or vice versa, but both, eventually). It's no longer germane if the OP is satisfied with a fixed 5.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 16th, 2006, 6:30 PM   #8
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Learn C++. Take your time. Research the problem in more detail. Write some code then come back if you have any problems. This is the expected approach if you want others to help you.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Feb 16th, 2006, 7:13 PM   #9
jobobshishkabob
Programmer
 
Join Date: Jan 2006
Location: Texas
Posts: 36
Rep Power: 0 jobobshishkabob is on a distinguished road
sorry but i have to turn this on the due date... saturday morning! so i dont have time to extensively learn c++. i wll after this project.... i just want a feel for it now.

thanks
jobobshishkabob is offline   Reply With Quote
Old Feb 16th, 2006, 7:23 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Forgive me, but it seems unlikely in the extreme that you were assigned this in the last day or two, and it's due Saturday, unless you've had much more preparation than you've taken advantage of.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC