Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a set of 10 symbols and a set of strings(at max 100) of length at max 20 each consisting of these symbols, find the maximum length string which can be made from these symbols that doesn't have any of the given strings as its sub-string. In case, if we can have infinite long string satisfying the property, print -1.
Besides brute force algorithm which will go exponential in time, I am not able to get any solution for this.
Any hint to approach this problem will be thankful.
Given a set of strings that need to be matched, my immediate reaction is to use http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm to create a matcher. This matcher is a finite state machine that accepts one character at a time and tells you which state you end up in next, given that character.
So I think you can reduce the problem to accepting a directed graph and a starting point and finding the longest route through that graph that does not go through the nodes that correspond to pattern matches - which I think we can simply delete from the graph. This is covered in http://en.wikipedia.org/wiki/Longest_path. Constructing this graph is also linear so the whole thing seems to be O(n)
Related
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 5 years ago.
Improve this question
I would like to know about simple problems that can be naturally solved using stacks with the usual interface (emptyS, isEmptyS, push, pop, top).
The complexity asociated to the context of the problem should be null. I can't touch topics like parsing, compiling or search algorithms at this moment. This discards many classical examples.
The most beautiful example I found so far is checking for balanced parenthesis in strings. In very few lines, without any other background, the exercise shows the utility of the data structure:
Another good example is procesing a string where the asterisk means to pop an item from the stack and a letter means to push it into the stack. The function must return the stack after the operations described in the string are applied to an empty stack.
If you can share some others problems, I will apreciate it very much.
Thank you in advance.
Though this question is too broad, I am going to give some other applications. Some of other common applications are -
Parsing
Recursive Function
Calling Function
Expression Evaluation
Expression Conversion
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
Towers of Hanoi
Some details can be found here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to solve the following problem:
Give a linear-time algorithm to determine if a text T is a cyclic rotation of another string T'. For example, arc and car are cyclic rotations of each other.
I have no idea where to start. How can I solve this problem?
As a hint: if x and y have the same length, then x is a cyclic rotation of y iff x is a substring of yy. Try proving this and using this as the basis for your algorithm.
Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a python program that allows a user to input as many positive integers as they want. When the user inputs a positive or negative integer, the program generates a list of the inputted numbers that is sorted and also generates an average of the list inputted by the user.
Someone please help. I need to know this by this evening.
We don't do your homework here, but without divulging a complete solution, think about writing a loop to query the user for integers and add them to a Python list. You can make use of the sorted() and sort() functions in Python to sort your data. Averaging values in a list is trivial and can be easily found online or with basic programming knowledge (sum the values in the list and divide that total by the length of the list). Also beware of integer division depending on your version of Python.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I find if a string contains a contiguous palindromic sequence ? I could try the naive solution in O(n^2) time where n is the string size , but any efficient algos to it ?
Well looking for just any palindrome isn't particularly interesting since every one character string is a palindrome. If you are looking for the longest palindrome you may be interested in Manacher's Algorithm.
A good description of the algorithm can be found here.
This is a quite common problem, and has ample results on google:
http://en.wikipedia.org/wiki/Longest_palindromic_substring
Rather than using Manacher's Algorithm you should use one of the parallel algorithms.
duplicate of : how to find longest palindromic subsequence?
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
It seems excel knows how to calculate =cos(2^27-1) but fails to calculate =cos(2^27). That returns #NUM!. Does anyone know why?
I have no idea what sort of arithmetic that Excel uses internally, but at some point, with a large number, the error after you do a mod 2*pi operation is too substantial to produce a reliable answer. Presumably they picked 2^27 as their cutoff.
This is behavior is not well documented. The Sin Function documentation indicates that the argument is a Double, and the specified limits in the documentation indicate that the double type is stored as a 64-bit number ranging from 4.94E-324 to 1.797E308 (for positive numbers).
I suspect that it is not coincidental that 2^27 (134,217,728) bytes is precisely 128 megabytes, and it seems likely that there is an internal limitation for some trig functions (eg. COS, SIN and TAN, but interestingly, NOT for TANH, etc.). This is not to say that this amount of memory consumption would be required - it's just that a programmer's implementation could have some (potentially unnecessary) limits on these types of inputs internally.
To get around this silly limit, simply use the following:
=COS(MOD(2^27, 2*PI()))
This works because the limitation does not exist for other operations, and is nowhere to be seen in the Excel Specifications and Limits. :-)
It would be good for the documentation as linked provided a description of these limits, but unfortunately, it does not.