Consider a valid string with bit-wise operands (that is "xor" ,"or","and") and also "X" where X is
a integer from {0,1}. Let us consider their are n number of "X" then their are 2**n possibles of strings .we need to find number of strings that have value as "0" and value as "1".
Example :
Consider "X&X" out of 4 possible,only 3 have value 0 and only one has value 1.
Can anybody help me when their are more than one bit wise operand in string.
You don't say, but I assume that the interpretation of strings is done by associating left-to-right (that is 0^1&0 means (0^1)&0). I'll use |, & and ^ for or, and, xor.
Let s be an arbitrary string with n Xs that evaluates to 0, and t be an arbitrary string with n Xs that evaluates to 1.
Then, the strings with n+1 Xs that evaluate to 1 look like one of: t|0, s|1, t|1, t&1, s^1, t^0. The ones that evaluate to 0 are s|0, s&1, s&0, t&0, s^0, t^1.
Fixing a sequence of operators, let S(n) be the number of strings with n Xs that evaluate to 0 with the given operators, and T(n) be the number that evaluate to 1. Suppose the i'th operator that you're given is O(i). Then, just counting the combinations in the previous paragraph for each operator, you have recurrence relations:
S(1)=T(1)=1
S(n+1), T(n+1) = S(n), S(n)+2T(n) if O(i)=|
S(n+1), T(n+1) = 2S(n)+T(n), T(n) if O(i)=&
S(n+1), T(n+1) = S(n)+T(n), S(n)+T(n) if O(i)=^
These recurrence relations are easy to encode in an O(n) dynamic program.
Related
Suppose we have a boolean array of length X. The only rule is, TRUE must not occur twice in adjacent places. Especially the array with only false values is allowed. E.g. this is forbidden: [1,1,0,0,0] and these is allowed: [1,0,0,0,0], [0,0,0,0,0], [1,0,1,0,1] etc. How can I use dynamic programming to determine how many different valid arrays of length X there are?
Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.
Then:
T0 = 0
F0 = 1
Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)
Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)
You want FX + TX.
So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.
(This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)
I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.
Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases
1. the array has no 1's, only one case, and it is a valid array
2. the array has one 1's, all cases are valid
3. the array has two 1's, there are N - 1 cases which are not valid
4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid
...
The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.
You don't really need dynamic programming.
For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.
X=1: valid arrays: 2
X=2: valid arrays: 3
X=3: valid arrays: 5
X=4: valid arrays: 8
and so on...
Demonstration:
Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
And that's exactly the definition of the Fibonacci array.
All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.
You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).
I read that the total number of substrings that can be formed from a given string is n^2 but I don't understand how to count this.
By substrings, I mean, given a string CAT, the substrings would be:
C
CA
CAT
A
AT
T
The total number of (nonempty) substrings is n + C(n,2). The leading n counts the number of substrings of length 1 and C(n,2) counts the number of substrings of length > 1 and is equal to the number of ways to choose 2 indices from the set of n. The standard formula for binomial coefficients yields C(n,2) = n*(n-1)/2. Combining these two terms and simplifying gives that the total number is (n^2 + n)/2. #rici in the comments notes that this is the same as C(n+1,2) which makes sense if you e.g. think in terms of Python string slicing where substrings of s can always be written in the form s[i:j] where 0 <= i < j <= n (with j being 1 more than the final index). For n = 3 this works out to (9 + 3)/2 = 6.
In the sense of complexity theory the number of substrings is O(n^2), which might be what you read somewhere.
You have a starting point and and end point - if each could point to anywhere along the word, each would have n possible values, and therefor an overall of n^2, so that's an upper limit.
However, we need a constraint saying that the substring cannot end before it started, so end - start >=0. This cuts the possible count in about half, but on asymptotic terms it's still O(n^2)
Substring calculation is logically
selecting 2 blank spaces atleast one letter apart.
a| b c | d = substring bc
| a b c |d = substring abc.
Now how many ways can you chose these 2 blankspace. For n letter word there are n+1.
Then first select one = n+1 ways
Select another (not the same)= n
So total n(n+1). But you have calculated everything twice. So n*(n+1)/2.
Programmatically, without applying any special algorithms(like Z algo etc) you can use a map to calculate no of distinct substrings.(O(n^3)).
You can use suffix tree to get O(n^2) substring calculaton.
To get a substring of a given string s, you just need to select two different points in the string. Let s contain n characters,
|s[0]|s[1]|...|s[n-1]|
You want to choose two vertical bars to get a substring. How many vertical bars do you have? Exactly n+1. So the number of sustrings is C(n+1,2) = n(n+1)/2, which is to choose 2 items from n+1. Of course, it could be denoted as O(n^2).
Number prodigy is given X - there's a X digit number N, reverse of N is M. Number prodigy is interested in finding out how many X digit numbers are of form : N+M=10^X-1 and N is expected not have trailing zeroes. Means that N%10 != 0 .
In case of X=1, 9 such combinations exist.
Denote A[i] - the i'th digit of A.
We first need to understand that to get N+M=10^X-1, we need N[i]+M[i]=9 for all i. Since M[i]=N[X-i], it means we need N[i] + N[X-i] = 9. This means, once N[i] is set, also N[X-i].
We can now derive a recursive formula:
F(X) = 10*F(X-2)
The idea is - we look at the first digit of X, we have 10 possibilities for it, and for each possibility, we set N[0] and N[X-1].
However, this allows leading and trailing zeros, which we don't want. The first and last number can be anything by 0.
G(X) = 8*F(X-2)
The above is chosing one of 1,2,...,8 as N[0], then setting (one option) the last number so N[X-1] = 9 - N[0], and invoke the recursive call without restrictions. Note neither N[0] nor N[X-1] can be zero.
Base clauses:
F(0) = 1
F(1) = 0
F(1)=0 because there is no natural number n such that n+n=9.
All in all, we found a recursive formula to compute the total number of elements. This recursive formula can be transformed into a close one with some basic algebra. I leave this part for you.
I was wondering if anyone can answer me which is number of results generated in the backtracking solution for the next problem:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
There is a related post in stackoverflow: Generate balanced parentheses in java
My doubt if that if there is a formula that can give me the number of valid parentheses I can generate before compute them.
for example:
- f(n):
- f(1) = 1
- f(2) = 2
- f(3) = 5
and so on..
Thank you.
The number expressions containing n pairs of parentheses which are correctly matched can be calculated via Catalan numbers. Quoting the relevant link from Wikipedia:
There are many counting problems in combinatorics whose solution is given by the Catalan numbers … Cn is the number of Dyck words of length 2n. A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's … Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, Cn counts the number of expressions containing n pairs of parentheses which are correctly matched.
The nth Catalan number is given directly in terms of binomial coefficients by:
Given is a set S = {s1, ..., sm} where each si is a string of length k over the alphabet {0,1,?}.
I am looking for an efficient algorithm solving the following decision problem:
Is it true that for each 1 ≤ a < b ≤ k there is a string si in S s.t. si(a) = 0 and si(b) = 1 or si(a) = 1 and si(b) = 0, where si(a) denotes the a-th character in string si.
I am looking for a sublinear time algorithm in m, so something like O(\sqrt(m)f(k)) would be the goal.
Can't be done in less than linear time, unless prior offline processing is allowed.
Basically the first string that satisfies your criterion will be the last one you consider. And you might have to consider all m-1 others first.
Easy. Bitfield your character space (0 == 0x1, 1 == 0x2,...), then take the first N characters of each string in S and add their transformed representations, XOR the total against 0x3,..., and see if it evaluates zero.
For faster traversal than O(n), use this to form a binary search tree or heap (O(lg n) fetch), or a hash (O(1) fetch). If S is guaranteed to be sorted, this becomes even easier.
At least, if I understand your question properly. For better, more theoretic results with bounded mathematical complexity and proofs, Math.SE or CSTheory.SE are the go-to locations.