Matching partial string to complete strings from an array of strings - c

This is a small section of a question that I was asked once. I have a variable array of strings like
str list []={abcd,xyzw,qwer,abcde}
And my input is:
input list[]={ab,abc,q,z,x}
Output should be[]={abcd,abcd,qwer,-,xyzw}
Each input string should be matched to the same characters (from beginning) in the list. It should give the first available string as the answer.
The working approached I could think of were:-
Brute force: Time complexity O((number of strings in list)*(average length of input strings)*(number of input strings))
Hashing: It too is taking the same time.
Is there a better way to do this?

if your list[] is fix (or does not change very much), a trie would do the trick. While inserting, you have to do a bit of logic to identify the "first match", so if "abcd" is the first, you should not insert "abcde" (or invalidate it, if "abcd" could be discarded, so you have to do a bit of accounting and bookkeeping).
details: http://en.wikipedia.org/wiki/Trie

Related

Sorting an array of URLs

I have an array with quasar URLs stored in it
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0269/spec-0269-51581-0467.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0329/spec-0329-52056-0059.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/104/spectra/2957/spec-2957-54807-0164.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0342/spec-0342-51691-0089.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/2881/spec-2881-54502-0508.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0302/spec-0302-51616-0435.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/2947/spec-2947-54533-0371.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0301/spec-0301-51942-0460.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/104/spectra/2962/spec-2962-54774-0461.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/2974/spec-2974-54592-0185.fits
I want to sort out the URL array on basis of the number next to spec- and not using alphabetic order. I sorted the array with sort but it didn't help as it always took the 3rd row and 2nd last row to the top because they have a 1.
I'd like to have an output like this
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0269/spec-0269-51581-0467.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0301/spec-0301-51942-0460.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0302/spec-0302-51616-0435.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0329/spec-0329-52056-0059.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0342/spec-0342-51691-0089.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/2881/spec-2881-54502-0508.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/2947/spec-2947-54533-0371.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/104/spectra/2957/spec-2957-54807-0164.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/104/spectra/2962/spec-2962-54774-0461.fits
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/2974/spec-2974-54592-0185.fits
If you will always have this pattern, you can try:
fileName = strsplit(myUrl, '/')(end)
number = strsplit(fileName(5:end), '.')(0)
Gonna walk you through this cause understanding is everything...
We start with
http://dr12.sdss3.org/sas/dr12/sdss/spectro/redux/26/spectra/0269/spec-0269-51581-0467.fits
First we split the URL on the / characters. This will return a vector of strings split up from this character. Since the number to sort on resides after the final /, we can pass end to grab the last one. Now we have
spec-0269-51581-0467.fits
Next, let's remove that pesky spec- from the number. This step isn't actually necessary, since it's constant across all the URLs, but let's just do it for fun. We can use Matlab's substring to grab the characters after the -, using fileName(5:end). This will create a string starting with the 5th character (in this case, a 0) and continue to the end. Great, now we have
0269-51581-0467.fits
Looking good! Again, this part isn't completely necessary either, but just in case for whatever reason you may need to, I've included it. We can use the strsplit function again, but this time split on the ., and grab the first element by passing a 0. Now, we have
0269-51581-0467
Go ahead and sort that little guy and you're good to go!

AS3 sort string alphabetically

I'm working on a program in which a string contains some words with line breaks between them:
Bob
Mohammed
Alfred
Moses
Tom
etc
What I'm trying to do is sort it alphabetically as such:
Alfred
Bob
Mohammed
Moses
Tom
...
As far as I know, it can only be done in arrays, but is it possible to sort items inside a string?
When there isn't a pre-built function to do something, then just make your own function for it. This is critical to programming in general.
Remember what a string is: it's an array of characters. In ActionScript 3.0, this isn't quite as true within the language's actual syntax, but you can get around it pretty easily. String.charAt() gets you the character at the given index, and you have a whole slew of operations to actually change the order or number of characters in a string.
That being said, since you're just trying to sort individual words, and since they're already one-to-a-line, you can split on the newline character, "\n", which will create an array. Then you sort that array. I don't recall off the top of my head whether an array in AS3 will natively sort strings in alphabetical order - you might want to Google that - but I think it does; if not, then that just goes back to what the first paragraph was saying. Finally you join the array on the same character.
So:
var arr:Array = nameString.split("\n");
// sort the different elements within arr here; each one is one name
nameString = arr.join("\n");
You can create an array out of the string by splitting on the newline character (\n), then sorting it, and converting it back to a string by joining the elements with the same newline character:
var str:String = "Bob\nMohammed\nAlfred\nMoses\nTom";
var sortedString = str.split("\n").sort().join("\n"));

compare string to string list in c

in my C application, I have a string which I want to compare to a list of strings in my application. the list of strings can be hardcoded in my C application.
what is the best way to define the list of strings in my application?
what is the best way to compare the string in my application to the list of strings?
I you are after efficiency, you should use a trie, it will give you O(|S|) search time to match your input string to the given set of strings. [where |S| is the length of the input string]
If you are after quick coding, just store the strings in a predefined char*[], and iterate over it with strcmp()
What do you mean by "compare a string to a list of strings"? The two are different kinds of "objects", they are not obviously comparable.
How does the string "372" compare to the list of strings { "12, "42", "-11", "372" }?
If you mean "look for a string in a list of strings", then the best bet would be to sort the list, then use binary-search to quickly check if the candidate is present.
A list of strings is easily represented in C as an array of character pointers, char *strings[]. You can use qsort() to sort, and bsearch() to search.
If you want excessive speed, and you're willing to use extra tools, you could consider gperf. Given a list of strings, it works out a very fast inclusion test based on a hash function, and spits out the C code for it.

C - Arrays/Struct discussion

I have to organize a struct or an array of some people's first name, last name, and age then organize them in alphabetical order writing them from an input file to an output file using the string library.
This is partically for a Lab that I have tomorrow afternoon in which my TA said we might be asked to complete the task I stated above. I'm trying to get opinions or suggestions from individuals whom are much more experienced in C than myself so I'll be more prepared than I was last lab where I didn't so hot.
I'm stuck, any suggestions on where I should start?
Well, reading and writing the input and output should be a no brainer (you open the file, read/write and then close it when you're done with it).
The trick is to get those pesky strings sorted.
The way I look at strnig sorting is an array of arrays sorting. The first array-level is an iteration over all the structs while the second is an iteration over all the letters of a name (note that some names are longer then others)
1. First you sort the first column - all the first letters
2. Then all the sub-columns - within each first letter group you sort the second letter
3. Repeat 1&2 until you run out of letters to sort.
The sorting of an array of letters is the same as sorting an array of byte (AKA chars), and if you're working with an algorithm like bubble sort, working on a sub array is seamless.
Hope this idea helps
You can use strcmp from string.h library, which compares two strings and returns the result.
You should read every string from input file and keep in memory. You will probably use a list for that, for example linked list. While adding you can organize them in alphabetical order. Here is an example:
Take the an element in list.
Compare it with new string.
Where should it be placed? if it should be before the current element, place it before the current element. If it should be after current element, do the same for next element.
This is a simple algorithm for your problem.
The tricky part is to implement the comparation of two strings. the strcmp function only tells you whether the strings are identical. You can loop through the char array to do it.
Once this is done, the remaining sorting is simple.

Finding a match in an array using strcmp

im trying to compare words of an array using strcmp.Im trying to get each word that appears more than once in the array to print out only once, so i can determine the number of unique words.I know what its doing wrong as when it searches the array it prints out each copy it finds, for example if the word "the" is in the array 4 times, it will print out 'the' 3 times and when string1 goes to the next location where 'the' is, it will print out 2 times and so on.
Convert your char arrays to std::string and instead of printing them, put them into an std::set. Then print each element in the set.
good that you added declarations,
now from what it looks it seems as if words[][] is redundant and makes things unnecessary complicated. if you are only interested in getting unique words, instead just process what comes back from strtrok by building up a dictionary with the encountered words
a dictionary could be something as simple as a max sized array containing unique words, and an index that starts at 0 when array is empty, whenever strtok returns a word, go through the array and look for the word using your strcmp, if it doesn't exist add it at the end of the array then increment then index.
and bob is your uncle :)

Resources