Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
randomly came across this:
Develop an algorithm to compare two sentences to see if
they match or not. The key aspect of these sentences is that
the words could be in any order (e.g. "california is hot" and "
hot is california" are two sentences that would match).
any ideas?
Parse each sentence into words, use space as delimiters.
Add all std::string words to a std::vector<std::string>, then sort.
Use the ==operator to compare the two vectors for equality.
Perhaps put words into a std::map<string, int> and count up the element each time you find a word on the one side, and down on the other side, then iterate over the map and check that all entries are zero. [This assumes that "california is hot hot" isn't supposed to be the same as "hot is california", in which case you need a bit more logic, to only count words the first time you see them on each side]
Or put each word in each sentence into a std::vector<string>, then sort each vector and compare the two vectors. Again, strategy changes if the sentence needs to be recognised regardless of the number of times each word is seen.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm sorry if the title is undescriptive, I'm not sure how to summarize my issue into a few words.
I'm looking to find which characters are physically near other characters on my QWERTY (UK, but I don't mind if you provide information specific to US) keyboard.
e.g:
charsNearChar('j') // OUTPUT -> U,I,H,K,N,M.
I can't seem to wrap my head around any solutions beside switch cases for each individual character, any help is appreciated!
There is no (simple) calculation that you could perform to get the list of adjacent keys. You simply need to use an explicitly written list of adjacent keys for each key.
any solutions beside switch cases
You don't need switch cases. What you're essentially asking for is a graph where nodes are keys and edges are to "adjacent" keys.
There are many ways to represent graphs. For your use case, perhaps an easy to understand, and reasonably fast choice is to use an associative map from key to its adjacency list (a vector of chars, or a string):
std::unordered_map<char, std::string> {
{'J', "UIHKNM"},
....
};
Since you limit the functionality to alphanumeric keys, they have an interesting property of being in a hexagonal grid. Such grid could well be represented with a 2D matrix:
char grid[][] = {
"123...",
"QWE...",
"ASD...",
"ZXC...",
};
This representation has less repetition, and the adjacency lists can be generated form this matrix with an algorithm.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Please describe (without implementation!) algorithm (possibly fastest)
which receives string of n letters and positive integer k as
arguments, and prints the most frequent substring of length k (if
there are multiple such substrings, algorithm prints any one of them).
String is composed of letters "a" and "b". For example: for string
ababaaaabb and k=3 the answer is "aba", which occurs 2 times (the fact
that they overlap doesn't matter). Describe an algorithm, prove its
correctness and calculate its complexity.
I can use only most basic functions of C++: no vectors, classes, objects etc. I also don't know about strings, only char tables. Can someone please explain to me what the algorithm would be, possibly with implementation in code for easier understanding? That's question from university exam, that's why it's so weird.
A simple solution is by trying all possible substrings from left to right (i.e. starting from indices i=0 to n-k), and comparing each to the next substrings (i.e. starting from indices j=i+1 to n-k).
For every i-substring, you count the number of occurrences, and keep a trace of the most frequentso far.
As a string comparison costs at worst k character comparisons, and you will be performing (n-k-1)(n-k)/2 such comparisons and the total cost is of order O(k(n-k)²). [In fact the cost can be lower because some of the string comparisons may terminate early, but I an not able to perform the evaluation.]
This solution is simple but probably not efficient.
In theory you can reduce the cost using a more efficient string matching algorithm, such as Knuth-Morris-Pratt, resulting in O((n-k)(n+k)) operations.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to create a program in C++ that arranges inputted words in alphabetical order. Can any one help.
I tried arranging taking first letter using for loop but so far it gives me absurd result. I only want a little hint and will do the rest on own
Yes this is easy. The standard library does all the work for you. The string has an operator< which is compared lexicographically. So really, it's a waste of time. std::sort will call operator< on its parameters.
Warning: by default, lexicographically means it will compare the ASCII values. So the exclamation mark goes before numbers, and numbers go before capital letters, and capital letters go before lowercase letters.
string myWords[10];
for (int i = 0; i < 10; i++)
cin >> myWords[i];
sort(begin(myWords), end(myWords));
If you have a vector, just do v.begin() and v.end().
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So if i have a large text file with lines that repeat, how can I determine the line that repeats the most frequently?
Example
The dog is brown
The cat is orange
The dog is brown
This should return 2 for The Dog is brown
Since this appears to be a learning exercise, here is an approach that you can take:
Make an associative container, say, map<string,int> or unordered_map<string,int> to keep counts
Read the file line-by-line. For each line that you get increment the count in your associative container
Once you finished reading the file, walk the container, find the highest count, and store its associated key
When you are done with the loop, the key that you found and its associated count give the answer to your problem.
One way to understand this approach is to consider the same problem, but with strings replaced with numbers. Now all you need to do is writing a loop performing count[number]++ for each number in the list. The approach described above is essentially the same thing, but you use a string instead of the number for your index.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hey I'm trying to make a function, the function its self finds the word with the highest score and then list that word, I'm currently using a vector to find the word with the highest score but I wish to get the word from my dictionary.
The vector you store your words in is a vector of std::string, and not a vector of Word pointers. So you can't use compare_by_score as it expects the latter kind of arguments and not the first.
Either store Word pointers in the vector, or change the compare_by_score function to take const std::string& arguments and then in the function convert it to Word objects.