Matching a line to a rule in c++ [closed] - string

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a line and a set of rules (other lines). Line to match (line = rule) may be very long; set of rules may be large and each rule may be long. Shorter rules may be part of longer (need to choose longer).
Currently, I have about 70 rules, <30 characters long, organized in a long if-else-if chain.
Is there any way to predict at what point there will be decrease in performance?
Is there a faster way of matching that comparing line with each of the rules?
Edit: There are no text files. I have an encoded sequence of characters, I go through if-elses comparing to "rules", and then act accordingly.

If you want to just check whether the input line is equal to any of the lines rules, then use an std::set (or std::map, if you want different behavior per rule) to store them. That takes the matching complexity down to O(lg N) where N is the number of rules.
Better yet, use an unordered_set (C++11) for O(1) performance.
If the behavior does not depend on which rule matched, then you can also compile a regular expression from the rules (e.g. (niVVVd__xniVVd__)|(niVVVdxniVVd)) with a tool like RE2 to get worst-case O(n) behavior, where n is the length of the input string.
Since you're comparing for equality, you don't need to match the longest rule first.

Related

want someone to do my homework for me [closed]

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 6 years ago.
Improve this question
# Change this program to output each letter of the text entered on a separate line
# with space in front of each letter, increasing to the right for each letter.
# For example, 'Python' input should output:
# P
# y
# t
# h
# o
# n
text = input('Enter text: ')
for letter in text:
print(letter)
I already tried to look online for the solution, there are none.
This code is for homework but i cant figure it out help wold be appreciated
As you haven't told us what you have tried to do so far, or what you have tried to learn to figure it out, I'm not going to post the Python code. Instead, lets think about what the program should be doing. Your assignment statement gives a broad overview, but as a programmer you need to take this overview and turn it into a set of smaller instructions. These smaller steps do not have to be in code. They can be in whatever form you like, even plain english.
For functional analysis (which is what you are doing for this problem) start with the inputs and outputs then fill in the stuff in-between.
1) Input: a string
X) Output: multiple lines with a single character and whitespace
Now how do you want to get from 1 to X. You already have the code to loop through each letter and print it, but you are missing two things to get to your required output.
A) way to place a string on a new line
B) way to add whitespace to a string
I'll give you a couple of hints. A) is something that is extremely common in almost any programming language. In fact, it is done the exact same way is any language that you are likely to use. Another hint. Your final output will be a single string that spans multiple lines. Ask yourself how a word processor or text editor gives works with blank lines.
B is a little more tricky, as there are a couple of nifty Python tricks that makes it simpler to do than in other languages. I'm guessing that you already know what happens when you add two numbers together such as 3 + 5. But what happens when you add two strings together such as "foo" + "bar". Notice how the behavior for adding numbers with the + operator is completely different than the behavior is for adding strings together with the same operator? That difference in behavior applies to other common operators as well. Play around with the other three common mathematical operators on both string and numbers to see what happens

What are these ? ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
These "characters" seem interesting. What are they?I would like to know more technical information about them. Thanks!
ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉ ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉ ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉
It's just a mass of combining marks. That alone is like 170 Combining Marks. You can do all sorts of weird rendering effects just by using a ton of combining marks together. But in this case just U+0489 COMBINING CYRILLIC MILLIONS SIGN is used.
Combining marks are not stand alone characters by themselves, but modify the previous base letter. For example a, a combining diaeresis ¨ renders as ä instead of a¨. Put more combining marks in there, and they all have to somehow render with the base character a, causing interesting rendering effect.
Code in the jsfiddle (SO didn't allow me to post otherwise):
var l = 1000;
var str = ":"
while(l--) {
str += String.fromCharCode(0x300 + Math.floor((Math.random() * 0x20)))
}
document.write(str);
Those are unicode characters that belong to another character set/language (in this case, Cyrillic). This information can be seen (on Windows) by using the Character Map tool, which is found in the Programs -> Accessories folder of the Start Menu.

Searching for a "needle" in a two dimnesional "haystack" [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I guess this is one of the most commonly asked interview questions, yet I am unable to solve it in an efficient way(efficient meaning lesser time complexity and use of a suitable Data Structure).
The problem is this way:
If there is a m x n matrix of chars (say haystack) and a given char string of length k (the needle) . Write a program to check if the haystack contains the needle. Please note that we need to search the haystack only top to down or left to right.
For example
Haystack
ahydsfd
sdflddl
dfdfd
dfdl
uifddffdhc
Needle:
hdffi
Output:
Yes Found!!
The naive bruteforce is O(m*n*k). Here are some ideas for optimization.
Single Search
Instead of doing a search for horizontals and then another for verticals, do both simultaneously. Every time you find an occurrence of the first letter of the needle look for a horizontal and a vertical match starting at that letter. This won't improve the time complexity, but could halve the time since you'll only look at bad starts once.
Rare Letters
Instead of looking for the first letter of the needle, look for the rarest letter which occurs in the needle. This could rule out a lot of the possible matches quickly (though it won't improve the worst-case time complexity). To determine which letters are rarest either scan through the entire board or use random sampling.
Efficient String Searching Algorithm
String searching is a well-studied problem and there are several linear-time algorithms such as Knuth–Morris–Pratt and Boyer–Moore. Using a linear-time string searching algorithm to search each each row and each column reduces the time complexity to O(m*n). This is probably what the interviewers are after.
Exploit Short Rows
I notice that not all rows have the same length. When you look for vertical matches, you can stop searching on that row as soon as the needle 'pops out' of the sack, since all needles further along the row will also exit the sack and therefore cannot match.
The brute force method will have worst time complexity of m*n.That is if needle is single character and we start parsing matrix row wise or column wise.
You can restrict the search of the first char to n-k columns and m-k rows. Once found, 2(k-1) would comparisons are required for the answer.

Is there any case where you must use ++var? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
The question is whether there is a situation where you can not use var++ and must use ++var (or the other way around).
That is, as far as I know, the only reason to use ++var over var++ (or the other way around) is to save space.
The question is, can we just have only the var++ abillity and not lose anything in the language's power?
You have to use ++var if you're using the expression within a bigger expression, and you definitely want the value of the expression to be the incremented one. For example, in C#:
int foo = 0;
foreach (var x in someCollection)
{
Console.WriteLine("{0}: {1}", x, ++foo);
}
Of course you could change this to use foo++ in a separate statement, but that isn't always feasible:
int foo = 0;
Expression<Func<int>> expression = () => ++foo;
That will return 1 the first time you call it, and you can't convert statement lambdas to expression trees.

what is logic programming? and what is different from other [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
today i have talk with other friend ,he said he has logic programming skill , so I am very curious about that.
The wikipedia entry explains it well: while on the surface it seems a redundant terms since all programming uses logic, in practice it's term for a well-defined paradigm, like, say, "functional programming" and "object-oriented programming". Specifically,
logic programming, in the narrower
sense in which it is more commonly
understood, is the use of logic as
both a declarative and procedural
representation language. It is based
upon the fact that a backwards
reasoning theorem-prover applied to
declarative sentences in the form of
implications:
If B1 and … and Bn then H
treats the implications as
goal-reduction procedures:
to show/solve H, show/solve B1 and … and Bn.
The language Prolog (in some variant or other) is probably still the most popular logic programming language.
I'd usually understand it to mean using prolog. Prolog allows you to define predicates, and truth values. The prolog interpreter can then derive further "truths", using standard logic rules. For example, each of the following lines establish a father_child and mother_children relationship between the first and the second parameter (the people mentioned are from the Simpsons).
member(X, [X|_]).
member(X, [_|T]) :- member(X,T).
mother_children(marge, [bart, lisa, maggie]).
mother_children(mona, [homer, jay]).
mother_child(X, Y) :- mother_children(X, C), member(Y, C).
father_child(homer, bart).
father_child(homer, lisa).
father_child(homer, maggie).
father_child(abe, homer).
father_child(abe, herb).
father_child(abe, abbie).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
If you fire this program into a prolog interpreter, and then ask it sibling(X,Y), it will return to you all the pairs of siblings that exist. What's interesting is that we never explicit say that say, Bart is a sibling to Lisa. We just define father and mother relationships, but by defining further rules, prolog uses normal rules to derive what names fullfill the sibling rule.
Prolog was more popular in the 80s, in various AI systems and the like. It's a bit out of fashion these days (not that you'd know in universities, where it's still hot shit).
I would take that statement to mean, "I can program if/then/else statements". Or in other words, I would take that statement to mean, "I can't program with any real technology". I would not be impressed.

Resources