473,785 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strategy for locating strings based on initial characters

Hello.

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search substring
which should match the beginning of the searched item? The actual goal
is to implement a functionality similar to that found in help indices,
where one can locate an item by gradually typing its initial characters.
I expect that some kind of tree structure be present in the solution,
but I am not sure.

Since I am multiposting this to comp.programmin g and comp.lang.c++, I
would like to make it on-topic on the latter as well by asking whether
the STL provides any algorithms that could help here, assming, for
instance, that the strings are stored in a std::set or sorted std::vector.

Thank you,

--
Ney André de Mello Zunino
Jul 23 '05 #1
4 1664
Ney André de Mello Zunino wrote:

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search
substring which should match the beginning of the searched item?
The actual goal is to implement a functionality similar to that
found in help indices, where one can locate an item by gradually
typing its initial characters. I expect that some kind of tree
structure be present in the solution, but I am not sure.


Look up "trie".

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Jul 23 '05 #2
Ney André de Mello Zunino wrote:
Hello.

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search substring
which should match the beginning of the searched item? The actual goal
is to implement a functionality similar to that found in help indices,
where one can locate an item by gradually typing its initial characters.
I expect that some kind of tree structure be present in the solution,
but I am not sure.

Since I am multiposting this to comp.programmin g and comp.lang.c++, I
would like to make it on-topic on the latter as well by asking whether
the STL provides any algorithms that could help here, assming, for
instance, that the strings are stored in a std::set or sorted std::vector.

Thank you,


I needed something similar at work. My goal was to lookup an object
based on an object name. My implementation used the STL map<key,object> ,
because the map stores object sorted and is therefore 2ln(n) for lookups.

The map needs a key that supports the < operator in order to sort the
objects. So I created a class called SortableString that implemented
the < operator using the c function strcmp.

So my template instantiation looked like
map<SortableStr ing, Object*> objectMap;

I believe this sort of implementation would work for what you want to
do. First you would populate the map with a set of SortableStrings with
their text set to the topics of your help articles. The objects would
be pointers to your articles.

When the user types in a partial string you would create a temporary
SortableString object with its text set to the what the user typed in.
You can then use the lower_bound() function to locate the first element
greater than your temporary string (which should be the closest match
amoung all your subject headers. Psuedocode below.

class SortableString {
SortableString( char* string);
~SortableString ();
bool operator < (SortableString & other);
};

map<SortableStr ing, ArticleObject*> subjectHeaderMa p;
bool AddSubject(char * subject, ArticleObject* article)
{
SortableString* newSubject = new SortableString( subject);
bool rvalue = objectHeaderMap .insert(map<Sor tableString*,
ArticleObject*> ::value_type(ne wSubject,articl e).second;
return rvalue;
}

bool MatchSubject(ch ar *test_subject, SortableString* found_subject,
ArticleObject** found_article)
{
bool match_found = false;
SortableString* newSubject = new SortableString( subject);
map<SortableStr ing, ArticleObject*> ::Iterator iter;

iter = objectHeaderMap .lower_bound(ne wSubject);
if (iter != objectHeaderMap .end())
{
*found_subject = iter->first;
*found_article = iter->second;
match_found = true;
}

delete newSubject;
return match_found;
}
Jul 23 '05 #3
Ney André de Mello Zunino wrote:
Hello.

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search substring
which should match the beginning of the searched item? The actual goal
is to implement a functionality similar to that found in help indices,
where one can locate an item by gradually typing its initial characters.
I expect that some kind of tree structure be present in the solution,
but I am not sure.


Modified binary search should do the job. (Modified, because you're
looking for "first possible match" and "last ditto", and because it
*might* be useful to work incrementally, although I strongly suspect
that would be an unnecessary optimisation.)

--
Chris "electric hedgehog" Dollin
Jul 23 '05 #4

"Ney André de Mello Zunino" <zu****@inf.ufs c.br> wrote in message
news:d4******** **@domitilla.ai oe.org...
| Hello.
|
| Given a sorted collection of strings, what would a good (the best?)
| strategy be to allow fast access to an item, based on a search substring
| which should match the beginning of the searched item? The actual goal
| is to implement a functionality similar to that found in help indices,
| where one can locate an item by gradually typing its initial characters.
| I expect that some kind of tree structure be present in the solution,
| but I am not sure.
|
| Since I am multiposting this to comp.programmin g and comp.lang.c++, I
| would like to make it on-topic on the latter as well by asking whether
| the STL provides any algorithms that could help here, assming, for
| instance, that the strings are stored in a std::set or sorted std::vector.

As an idea, I would start by looking into the following:
std::string::fi nd_first_of( Criteria, StartPos );

std::string offers quite a few other members that may also help.

Cheers,
Chris Val
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3025
by: Steve Jorgensen | last post by:
I'm restarting this thread with a different focus. The project I'm working on now id coming along and will be made to work, and it's too late to start over with a new strategy. Still, I'm not coming to a firm conclusion over whether it was the better approach, and wonder if I should do it differently the next time I'm faced with a similar issue. I needed an app to automatically import from spreadsheets with a semi-dynamic structure,...
7
29279
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring with '\0', like that: table = "string0\0" table = "string1\0" ...
5
5878
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() = Microsoft.VisualBasic.Strings.Split(aLine, vbNewLine, -1, CompareMethod.Binary)
5
2230
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long list of songs and return any approximate matches along with a confidence factor. I have looked at edit distance, but that isn't a good choice for finding a short string in a longer one. I have also explored difflib.SequenceMatcher and...
19
3116
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde Nordrassil-Horde Draenor-Alliance Nordrassil-Alliance Nordrassil-Neutral Draenor-Horde
25
3415
by: marcin.rzeznicki | last post by:
Hello everyone I've got a little problem with choosing the best decoding strategy for some nasty problem. I have to deal with very large files wich contain text encoded with various encodings. Their length makes loading contents of file into memory in single run inappropriate. I solved this problem by implementing memory mapping using P/Invoke and I load contents of file in chunks. Since files' contents are in different encodings what I...
4
1582
by: dhinakar_ve | last post by:
Hi All, I am writing a function to generate the strings based on a pattern. For example A will generate A1, A2 and A3. If the pattern is A then it will generate the strings A11, A12, A21, A22, A31, A32. What is the best algorithm to accomplish this? Thanks for your time. ananihdv
3
9842
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.