I'm doing this exercise:
Write a program to get a number N and a sequence of numbers and characters. As a result the program must output first all numbers and then the symbols. All members must be separated by space.
So I wrote this code:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cctype>
using namespace std;
int main()
{
const size_t N = 1000;
char a[N];
size_t n = 0;
cin >> n;
if ( N < n ) n = N;
for ( size_t i = 0; i < n; i++ )
{
cin >> a[i];
}
auto it = stable_partition( a, a + n, []( unsigned char c ){ return ::isdigit( c ); } );
stable_partition( it, a + n, []( unsigned char c ){ return ::isalpha( c ); } );
for ( size_t i = 0; i < n; i++ )
{
cout << a[i] << ' ';
}
return 0;
}
Now when the program gets following input:
1000 < h X " O d ? 3 s P ? ^ ; p l | ^ ^ ) I & B " 2 h h v a a r M N 2 Q 2 a ! a ) N T s $ J [ Y X , 7 | = + m - f $ ] h c 0 x A 8 l d x m + C + y Z 9 U d E Y b = 6 ^ n ] y P 1 # Y ^ . U 1 e 1 3 w U d y E / M i L 1 T B Y . 7 t ' . o g g . ` ' & k # [ R % t O - \ # Y [ T k V ' H t L H I C _ t . K S l t y # B + - " U Z 4 B M l C 0 L ^ ] v c 0 B z K P . ~ i } y ^ q C V [ c 5 Y U L % N V - 8 < a [ y [ P J ) k Q I ` ~ q z d ) Y / Q b M [ h E 7 f { V S ( F K ` ? 4 ? _ C - + O X 6 n V a & $ p w C C D U 3 B y w : s M < z A D | 7 Q ? i & 4 9 > ! + e E [ # ; i b } $ # E L d ` w B 1 ~ N ; s x B 6 p W 7 g m o = _ 7 B C ' i 3 & Y r G 3 j g a Z j . ^ z ^ U . 1 7 > t [ o - G " b ~ ` ( h : R ~ R ~ F E l Q S 0 J j F ^ x 4 u K y 4 F | X C ~ B ? O r + } | " j A C % w ? v w | S E = c 4 6 . # H e q D a & 1 c % P | B " } g 0 9 | 7 n . # B 7 7 ` H v Q I i B g r r Q ; Q 5 c , ` C y x ( J m 1 W h # 6 I a X _ g l 9 j l # 8 > d < E ^ O { 2 _ l 8 u f L _ _ P F w > i ( x 5 4 f X 1 b 9 v S p I < V q 0 [ E I P j > ' $ h Q , I 5 7 R 5 I T T ; I R 4 V E ~ . Q " # n s u & 0 8 f 6 G U n ] 8 q u 6 7 j 2 T ! p ' " J ! t 5 E - E c ] r 2 : T ^ T j X V k 2 k [ z P R / m , / M ; I 5 d ( & b ^ Z m Z T " Q m t A a E d _ } j R H S [ o g | - ! F " $ J L E } J C b Z C a # & & r R T . " R o e V h / 9 t T < S ; l U 8 O q S _ Y t # L l 9 Y { { . o I Y G Y [ f Y \ K j S F ; r , O | 9 E 6 9 C T X p v G " ! ; M _ 3 K $ o $ . ] N q L > n = * M c a i x s J S v = # l D : ' 0 { f e d 7 1 E J G \ K L { v n C T / h y G c Q N s C C \ = & ` , 5 6 g G . P 6 - r > F z L s b ( c [ J P { . o T a 7 a / ? h 4 6 ; W $ h c i m f x , D l _ 6 9 C % l a L r q ' F 1 m } 5 l c 0 ( V I [ V Y . 0 G H 1 e j K = n p p b 9 v h V ; x - / o z R 3 n ' ; L P W } 3 > y Z = 8 9 ' - | x u , 2 l z t } | > w k 8 | P 8 , g k U ` 8 F , < 8 g & z c ^ Z 0 s < \ ^ D { 3 | ! C H R y b 3 ` C q I g p 0 $ e S * k e < 8 < q & - 1 A X < b / a K q x q w ) b I ? T h - e ! A . z J p U l } * P G H ! m - F ' = a : B ~ ! . k 4 A " U / Y c 4 b t u } < U K X y 1 H A # { E 3
It forgets to output symbols like < ` ".
As result I get false output with "eaten" symbols.
My program executes:
3 2 2 2 7 0 8 9 6 1 1 1 3 1 7 4 0 0 5 8 7 4 6 3 7 4 9 1 6 7 7 3 3 1 7 0 4 4 4 6 1 0 9 7 7 7 5 1 6 9 8 2 8 5 4 1 9 0 5 7 5 4 0 8 6 8 6 7 2 5 2 2 5 9 8 9 9 6 9 3 0 7 1 5 6 6 7 4 6 6 9 1 5 0 0 1 9 3 3 8 9 2 8 8 8 8 0 3 3 0 8 1 4 4 1 3 h X O d s P p l I B h h v a a r M N Q a a N T s J Y X m f h c x A l d x m C y Z U d E Y b n y P Y U e w U d y E M i L T B Y t o g g k R t O Y T k V H t L H I C t K S l t y B U Z B M l C L v c B z K P i y q C V c Y U L N V a y P J k Q I q z d Y Q b M h E f V S F K C O X n V a p w C C D U B y w s M z A D Q i e E i b E L d w B N s x B p W g m o B C i Y r G j g a Z j z U t o G b h R R F E l Q S J j F x u K y F X C B O r j A C w v w S E c H e q D a c P B g n B H v Q I i B g r r Q Q c C y x J m W h I a X g l j l d E O l u f L P F w i x f X b v S p I V q E I P j h Q I R I T T I R V E Q n s u f G U n q u j T p J t E E c r T T j X V k k z P R m M I d b Z m Z T Q m t A a E d j R H S o g F J L E J C b Z C a r R T R o e V h t T S l U O q S Y t L l Y o I Y G Y f Y K j S F r O E C T X p v G M K o N q L n M c a i x s J S v l D f e d E J G K L v n C T h y G c Q N s C C g G P r F z L s b c J P o T a a h W h c i m f x D l C l a L r q F m l c V I V Y G H e j K n p p b v h V x o z R n L P W y Z x u l z t w k P g k U F g z c Z s D C H R y b C q I g p e S k e q A X b a K q x q w b I T h e A z J p U l P G H m F a B k A U Y c b t u U K X y H A E < " ? ? ^ ; | ^ ^ ) & " ! ) $ [ , | = + - $ ] + + = ^ ] # ^ . / . ' . . ` ' & # [ % - \ # [ ' _ . # + - " ^ ] . ~ } ^ [ % - < [ [ ) ` ~ ) / [ { ( ` ? ? _ - + & $ : < | ? & > ! + [ # ; } $ # ` ~ ; = _ ' & . ^ ^ . > [ - " ~ ` ( : ~ ~ ^ | ~ ? + } | " % ? | = . # & % | " } | . # ` ; , ` ( # _ # > < ^ { _ _ _ > ( < [ > ' $ , ; ~ . " # & ] ! ' " ! - ] : ^ [ / , / ; ( & ^ " _ } [ | - ! " $ } # & & . " / < ; _ # { { . [ \ ; , | " ! ; _ $ $ . ] > = * = # : ' { \ { / \ = & ` , . - > ( [ { . / ? ; $ , _ % ' } ( [ . = ; - / ' ; } > = ' - | , } | > | , ` , < & ^ < \ ^ { | ! ` $ * < < & - < / ) ? - ! . } * ! - ' = : ~ ! . " / } < # {
Whereas the right answer is:
3 2 2 2 7 0 8 9 6 1 1 1 3 1 7 4 0 0 5 8 7 4 6 3 7 4 9 1 6 7 7 3 3 1 7 0 4 4 4 6 1 0 9 7 7 7 5 1 6 9 8 2 8 5 4 1 9 0 5 7 5 4 0 8 6 8 6 7 2 5 2 2 5 9 8 9 9 6 9 3 0 7 1 5 6 6 7 4 6 6 9 1 5 0 0 1 9 3 3 8 9 2 8 8 8 8 0 3 3 0 8 1 4 4 1 3 < h X " O d ? s P ? ^ ; p l | ^ ^ ) I & B " h h v a a r M N Q a ! a ) N T s $ J [ Y X , | = + m - f $ ] h c x A l d x m + C + y Z U d E Y b = ^ n ] y P # Y ^ . U e w U d y E / M i L T B Y . t ' . o g g . ` ' & k # [ R % t O - \ # Y [ T k V ' H t L H I C _ t . K S l t y # B + - " U Z B M l C L ^ ] v c B z K P . ~ i } y ^ q C V [ c Y U L % N V - < a [ y [ P J ) k Q I ` ~ q z d ) Y / Q b M [ h E f { V S ( F K ` ? ? _ C - + O X n V a & $ p w C C D U B y w : s M < z A D | Q ? i & > ! + e E [ # ; i b } $ # E L d ` w B ~ N ; s x B p W g m o = _ B C ' i & Y r G j g a Z j . ^ z ^ U . > t [ o - G " b ~ ` ( h : R ~ R ~ F E l Q S J j F ^ x u K y F | X C ~ B ? O r + } | " j A C % w ? v w | S E = c . # H e q D a & c % P | B " } g | n . # B ` H v Q I i B g r r Q ; Q c , ` C y x ( J m W h # I a X _ g l j l # > d < E ^ O { _ l u f L _ _ P F w > i ( x f X b v S p I < V q [ E I P j > ' $ h Q , I R I T T ; I R V E ~ . Q " # n s u & f G U n ] q u j T ! p ' " J ! t E - E c ] r : T ^ T j X V k k [ z P R / m , / M ; I d ( & b ^ Z m Z T " Q m t A a E d _ } j R H S [ o g | - ! F " $ J L E } J C b Z C a # & & r R T . " R o e V h / t T < S ; l U O q S _ Y t # L l Y { { . o I Y G Y [ f Y \ K j S F ; r , O | E C T X p v G " ! ; M _ K $ o $ . ] N q L > n = * M c a i x s J S v = # l D : ' { f e d E J G \ K L { v n C T / h y G c Q N s C C \ = & ` , g G . P - r > F z L s b ( c [ J P { . o T a a / ? h ; W $ h c i m f x , D l _ C % l a L r q ' F m } l c ( V I [ V Y . G H e j K = n p p b v h V ; x - / o z R n ' ; L P W } > y Z = ' - | x u , l z t } | > w k | P , g k U ` F , < g & z c ^ Z s < \ ^ D { | ! C H R y b ` C q I g p $ e S * k e < < q & - A X < b / a K q x q w ) b I ? T h - e ! A . z J p U l } * P G H ! m - F ' = a : B ~ ! . k A " U / Y c b t u } < U K X y H A # { E
How can I fix it?
Related
Given integer A and B, find integer X so that:
A,B < 2*1e18
A xor X = B + X
I highly doubt it is possible to solve this equation using maths. This is a coding problem I came across 3 years ago and even now I can't solve this myself.
My code so far:
(this is the brute force solution)
#include <iostream>
using namespace std;
int main()
{
unsigned long long a, b;
cin >> a >> b;
for (unsigned long long x = 1; x < max(a, b); x++) {
unsigned long long c = a ^ x;
unsigned long long d = b + x;
if (c == d) {
cout << x << endl;
break;
return 0;
}
}
cout << -1; //if no such integer exists
return 0;
}
Note that A + X == (A xor X) + ((A and X)<<1). So:
A xor X = A + X - ((A and X)<<1) = B + X
A - B = (A and X)<<1
And we have:
(A - B) and not (A<<1) = 0 (All bits in (A - B) are also set in (A<<1))
(A - B)>>1 = A and X
If the condition is met, for any integer Y that doesn't have bits that are set in A, (((A - B)>>1) or Y) is a solution. If you want just one solution, you could use ((A - B)>>1), where Y = 0. Otherwise there is no solution.
int solve(int a, int b){
int x = (a - b) >> 1;
if ((a ^ x) == b + x)
return x;
else
return ERROR;
}
It's not very hard, you just need to think small: suppose we are writing A, B and X in binary and Aᵢ is the value corresponding to the rightmost 2ⁱ bit.
We know that: Aₒ ⊕ Xₒ = Bₒ + Xₒ.
Let's use an example to discover how to evaluate that: A = 15 and B = 6. Converting to binary:
A = 1 1 1 1 B = 0 1 1 0
X = a b c d X = a b c d
Now we have some possibilities. Let's analyse the rightmost bits of A and B:
1 ⊕ d = 0 + d
We know that d can only be 0 or 1, so:
for d = 0
1 ⊕ d = 0 + d => 1 ⊕ 0 = 0 + 0 => 1 = 0 (not possible)
for d = 1
1 ⊕ d = 0 + d => 1 ⊕ 1 = 0 + 1 => 0 = 1 (not possible)
It's noticeable that XOR behaves just like binary sum (with the difference that XOR doesn't create a carryover for the next bit sum):
XOR SUM
0 ⊕ 0 = 0 | 0 + 0 = 0
0 ⊕ 1 = 1 | 0 + 1 = 1
1 ⊕ 0 = 1 | 1 + 0 = 1
1 ⊕ 1 = 0 | 1 + 1 = 0
so it won't be always possible to find a X that satisfies A ⊕ X = B + X, because there isn't a value d that satisfies 1 + d = 0 + d.
Anyway, if X exists, you can just find it out this way, from right to left, finding bit by bit.
WORKING FULL EXAMPLE
A = 15, B = 7:
A = 1 1 1 1 B = 0 1 1 1
X = a b c d X = a b c d
1 ⊕ d = 1 + d
Here, both d = 0 and d = 1 apply, then what? We need to check the next bit. Suppose d = 1:
A = 1 1 1 1 B = 0 1 1 1
X = a b c d X = a b c d
1 ⊕ d = 1 + d => 1 ⊕ 1 = 1 + 1 => 0 = 0 (possible)
BUT 1 + 1 = 0 generates a carryover for the next bit sum:
Instead of 1 ⊕ c = 1 + c, we have 1 ⊕ c = 1 + c (+1) =
1 ⊕ c = c (not possible)
so in this case, d must be 0.
carryover 0
A = 1 1 1 1 B = 0 1 1 1
X = a b 0 0 X = a b 0 0
-----------------------------------
0 0
we know that c must be 0:
carryover 0 0
A = 1 1 1 1 B = 0 1 1 1
X = a b 0 0 X = a b 0 0
-----------------------------------
1 1 1 1
but what about b? we need to check the next bit, as always:
if b = 0, there won't be a carryover, so we'll have:
1 ⊕ a = 0 + a (and this is not possible)
so we try b = 1:
1 ⊕ b = 1 + b => 1 ⊕ 1 = 1 + 1 => 0 = 0 (with carryover)
and now, for a:
carryover 1 0 0
A = 1 1 1 1 B = 0 1 1 1
X = a 1 0 0 X = a 1 0 0
-----------------------------------
0 0 0 0 0 0
1 ⊕ a = 0 + a (+1) => 1 ⊕ a = 1 + a
here a can be 0 and 1, but it must be 0, in order to avoid a carryover in the sum B + X.
Then, X = 0 1 0 0, thus X = 4.
CODE
#include <iostream>
using namespace std;
inline int bit(int a, int n) {
if(n > 31) return 0;
return (a & ( 1 << n )) >> n;
}
int main(){
int A = 19;
int B = 7;
int X = 0;
int carryover = 0;
int aCurrent, aNext, bCurrent, bNext;
for(int i = 0; i < 32; i++){
aCurrent = bit(A, i); bCurrent = bit(B, i);
aNext = bit(A, i + 1); bNext = bit(B, i + 1);
if(aCurrent == 0 && bCurrent == 0){
if(carryover) {X = -1; break;}
if(aNext != bNext){
X += 1 << i;
}
carryover = 0;
}
else if(aCurrent == 0 && bCurrent == 1){
if(!carryover) {X = -1; break;}
if(aNext == bNext){
X += 1 << i;
}
carryover = 1;
}
else if(aCurrent == 1 && bCurrent == 0){
if(!carryover) {X = -1; break;}
if(aNext != bNext){
X += 1 << i;
carryover = 1;
}
else {
carryover = 0;
}
}
else if(aCurrent == 1 && bCurrent == 1){
if(carryover) {X = -1; break;}
if(aNext != bNext){
X += 1 << i;
carryover = 1;
}
else {
carryover = 0;
}
}
}
if(X != -1) cout<<"X = "<<X<<endl;
else cout<<"X doesnt exist"<<endl;
return 0;
}
You can test it here.
I am trying to solve the following problem:
Find the smallest n-bit integer c that has k 1-bits and is the sum of two n-bit integers that have g, h bits set to 1. g, h, k <= n
To start with, n-bit integer here means that we may use all n bits, i.e. max. value of such an integer is 2^n - 1. The described integer may not exist at all.
It is obvious the case k > g + h has no solutions and for g + h = k the answer is just 2^k - 1 (first k bits are 1-bits, k - n zeroes in the front).
As for some examples of what the program is supposed to do:
g = h = k = 4, n = 10 :
0000001111 + 0000001111 = 0000011110
15 + 15 = 30 (30 should be the output)
(4, 6, 5, 10):
0000011110 + 0000111111 = 0001011101
30 + 63 = 93
(30, 1, 1, 31):
1 + (2^30 - 1) = 2^30
As I think of it, this is a dynamic programming problem and I've chosen the following approach:
Let dp[g][h][k][n][c] be the described integer and c is an optional bit for carrying. I try to reconstruct possible sums depending on the lowest-order bits.
So, dp[g][h][k][n + 1][0] is the minimum of
(0, 0): dp[g][h][k][n][0]
(0, 0): 2^n + dp[g][h][k - 1][n][1]
(1, 0): 2^n + dp[g - 1][h][k - 1][n][0]
(0, 1): 2^n + dp[g][h - 1][k - 1][n][0]
Similarly, dp[g][h][k][n + 1][1] is the minimum of
(1, 1): dp[g - 1][h - 1][k][n][0]
(1, 1): dp[g - 1][h - 1][k - 1][n][1] + 2^n
(1, 0): dp[g - 1][h][k][n][1]
(0, 1): dp[g][h - 1][k][n][1]
The idea isn't that hard but I'm not really experienced with such things and my algorithm doesn't work even for simplest cases. I've chosen top-down approach. It's hard for me to consider all the corner cases. I do not really know if I've properly chosen base of recursion, etc. My algorithm doesn't even work for the most basic case for g = h = k = 1, n = 2(the answer is 01 + 01 = 10). There shouldn't be an answer for g = h = k = 1, n = 1 but the algorithm gives 1(which is basically why the former example outputs 1 instead of 2).
So, here goes my awful code(only very basic C++):
int solve(int g, int h, int k, int n, int c = 0) {
if (n <= 0) {
return 0;
}
if (dp[g][h][k][n][c]) {
return dp[g][h][k][n][c];
}
if (!c) {
if (g + h == k) {
return dp[g][h][k][n][c] = (1 << k) - 1;
}
int min, a1, a2, a3, a4;
min = a1 = a2 = a3 = a4 = std::numeric_limits<int>::max();
if (g + h > k && k <= n - 1) {
a1 = solve(g, h, k, n - 1, 0);
}
if (g + h >= k - 1 && k - 1 <= n - 1) {
a2 = (1 << (n - 1)) + solve(g, h, k - 1, n - 1, 1);
}
if (g - 1 + h >= k - 1 && k - 1 <= n - 1) {
a3 = (1 << (n - 1)) + solve(g - 1, h, k - 1, n - 1, 0);
}
if (g + h - 1 >= k - 1 && k - 1 <= n - 1) {
a4 = (1 << (n - 1)) + solve(g, h - 1, k - 1, n - 1, 0);
}
min = std::min({a1, a2, a3, a4});
return dp[g][h][k][n][c] = min;
} else {
int min, a1, a2, a3, a4;
min = a1 = a2 = a3 = a4 = std::numeric_limits<int>::max();
if (g - 2 + h >= k && k <= n - 1) {
a1 = solve(g - 1, h - 1, k, n - 1, 0);
}
if (g - 2 + h >= k - 1 && k - 1 <= n - 1) {
a2 = (1 << (n - 1)) + solve(g - 1, h - 1, k - 1, n - 1, 1);
}
if (g - 1 + h >= k && k <= n - 1) {
a3 = solve(g - 1, h, k, n - 1, 1);
}
if (g - 1 + h >= k && k <= n - 1) {
a4 = solve(g, h - 1, k, n - 1, 1);
}
min = std::min({a1, a2, a3, a4});
return dp[g][h][k][n][c] = min;
}
}
You can construct the smallest sum based on the bit counts g, h, and k, without doing any dynamic programming at all. Assuming that g ≥ h (switch them otherwise) these are the rules:
k ≤ h ≤ g
11111111 <- g ones
111100000111 <- h-k ones + g-k zeros + k ones
1000000000110 <- n must be at least h+g-k+1
h ≤ k ≤ g
1111111111 <- g ones
11111100 <- h ones + k-h zeros
1011111011 <- n must be at least g+1
h ≤ g ≤ k
1111111100000 <- g ones + k-g ones
1100000011111 <- g+h-k ones, k-h zeros, k-g ones
11011111111111 <- n must be at least k+1, or k if g+h=k
Example: all values of k for n=10, g=6 and h=4:
k=1 k=2 k=3 k=4
0000111111 0000111111 0000111111 0000111111
0111000001 0011000011 0001000111 0000001111
---------- ---------- ---------- ----------
1000000000 0100000010 0010000110 0001001110
k=4 k=5 k=6
0000111111 0000111111 0000111111
0000001111 0000011110 0000111100
---------- ---------- ----------
0001001110 0001011101 0001111011
k=6 k=7 k=8 k=9 k=10
0000111111 0001111110 0011111100 0111111000 1111110000
0000111100 0001110001 0011000011 0100000111 0000001111
---------- ---------- ---------- ---------- ----------
0001111011 0011101111 0110111111 1011111111 1111111111
Or, going straight to the value of c without calculating a and b first:
k ≤ h ≤ g
c = (1 << (g + h - k)) + ((1 << k) - 2)
h ≤ k ≤ g
c = (1 << g) + ((1 << k) - 1) - (1 << (k - h))
h ≤ g ≤ k
c = ((1 << (k + 1)) - 1) - (1 << ((g - h) + 2 * (k - g)))
h + g = k
c = (1 << k) - 1
which results in this disappointingly mundane code:
int smallest_sum(unsigned n, unsigned g, unsigned h, unsigned k) {
if (g < h) {unsigned swap = g; g = h; h = swap;}
if (k == 0) return (g > 0 || h > 0 || n < 1) ? -1 : 0;
if (h == 0) return (g != k || n < k) ? -1 : (1 << k) - 1;
if (k <= h) return (n <= h + g - k) ? -1 : (1 << (g + h - k)) + ((1 << k) - 2);
if (k <= g) return (n <= g) ? -1 : (1 << g) + ((1 << k) - 1) - (1 << (k - h));
if (k < g + h) return (n <= k) ? -1 : (1 << (k + 1)) - 1 - (1 << (2 * k - g - h));
if (k == g + h) return (n < k) ? -1 : (1 << k) - 1;
return -1;
}
Some example results:
n=31, g=15, h=25, k=10 -> 1,073,742,846 (1000000000000000000001111111110)
n=31, g=15, h=25, k=20 -> 34,602,975 (0000010000011111111111111011111)
n=31, g=15, h=25, k=30 -> 2,146,435,071 (1111111111011111111111111111111)
(I compared the results with those of a brute-force algorithm for every value of n, g, h and k from 0 to 20 to check correctness, and found no differences.)
I'm not too convinced about the dynamic programming approach. If I understand correctly, you would need to define how to go to dp[g + 1][h][k][n], dp[g][h + 1][k][n], dp[g][h][k + 1][n] and dp[g][h][k][n + 1], with and without the carry bit, in function of previous computations, and I'm not sure about what are the right rules for all of those.
I think an easier way to think of the problem is as an A* search tree, where each node contains two partial candidate numbers to add, let's call them G and H. You start with a node with G = 0 and H = 0 at level m = 0, and work as follows:
If G + H has n or fewer bits and k 1 bits, that's the solution, you found it!
Otherwise, if
n - m < number of 1 bits in G + H - k
discard the node (no solution possible).
Otherwise, if
(g + h) - (number of 1 bits in G + number of 1 bits in H) < k - number of 1 bits in G + H
discard the node (not viable candidates).
Otherwise, branch the node into a new level. Generally you make up to four children of each node, prefixing G and H with 0 and 0, 0 and 1, 1 and 0 or 1 and 1 respectively. However:
You can only precede G with a 1 if the number of 1 bits in G is fewer than g, and similarly for H and h.
At level m (G and H have m bits), you can only precede G with a 0 if
n - m > g - number of 1 bits in G
and similarly for H and h.
If G == H and g == h, you can skip one of 0 and 1 and 1 and 0, since they will lead to the same subtree.
Continue to the next node and repeat until you find a solution or you don't have any more nodes to visit.
The order in which you visit the nodes is important. You should store the nodes in a priority queue/heap such that the next node is always the first node that could potentially lead to the best solution. This is actually easy, you just need to take for each node G + H and prefix it with the necessary number of 1 bits to reach k; that's the best possible solution from there.
There are possibly better rules to discard invalid nodes (steps 2 and 3), but the idea of the algorithm is the same.
I need to generate a string from random characters in range of a-Z. How can i do it?
Atm, i can only suggest randoming numbers in range of 1-52 and then reinitialize numbers with charecters. But that sounds awful.
CALL RANPERM seems well suited for this task.
25 data shuffle(keep=a: drop=addr);
26 array a[52] $1;
27 addr = addrlong(a1);
28 call pokelong(cat(collate(65,90),collate(97,122)),addr,52);
29 put #20 a[*];
30 seed=1234567890123;
31 do n=1 to 10;
32 call ranperm(seed,of a[*]);
33 put seed= #20 a[*];
34 output;
35 end;
36 stop;
37 run;
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
seed=295748501 i U I K e d H R b E D k u N c v B M T Z G V n A s W h r q Q j F J o x X O p g t Y w z P C m S l L f a y
seed=766887342 z F v U N G M q u H P Q Z d y b X l a D f p Y w m o r V A B n J I j e g c S x R T K O L i s k C h E W t
seed=618158873 L v x k T r c m S e F w n Z K G P t q E W C h M z X p d N o H Y V i s Q u y f B J O g l U R D b I a j A
seed=632090736 J l H Y T R A p N x C v U b w K V W d X t L y E G o Q B M z I S n q k f s O F P D j a r m i c g Z h e u
seed=1911942895 t G j Z e K q H x m L F A g T h v W a y V C B M k I r J S R P s Y Q p w O D E l X U o z u N d f n i b c
seed=479260872 Y Q i d P R z S G B a D K Z X j M E b h T m c e C I n x y w o A k r U f H u l N W s v J p L q O t g V F
seed=1143672881 L X i f a u m K T g x p E M o B q Z s c t A Q I P G D z U d H S y V J F j h r e l v C b k Y W R O w N n
seed=869134054 F D U g G w A Y J o e p r b H z q d l I L M v R W h y m t S n V x T P C K B k O Q i j s c N f Z a X E u
seed=481233318 n h P j V M y S W s m F a G l q f d o O A u T I E p k z b e C X t v Q H i w c U g Z K B R N L x r D J Y
seed=1445828380 d P t o N h c R r M l n E y g F x z S a W L G p w T Q k B H e v I K i b f q X C m Y D J Z V j A U u O s
You can use the byte function, along with a random number, to generate a random character. Just loop the desired number of times and combine the results with call cats. The byte function returns the relevant character from the ASCII table. For your example you need ASCII numbers 65-90 (A-Z) and 97-122 (a-z). The following code will generate a random sequence of 6 characters.
data _null_;
length z $6 y $1;
do x=1 to 6;
do until (rank(y) in (65:90,97:122)); /* ASCII characters A-Z, a-z */
y=byte(65+floor((1+122-65)*rand("uniform"))); /* Generate random integer between 65 and 122 */
end;
call cats(z,y); /* concatenate values */
end;
putlog z;
run;
This is my current code on GAMS. Yes, I know it can be more concise, but bear with me here:
set activity /a*q/;
parameter duration (activity) "in days"
/a 15, b 4, c 5, d 10, e 4, f 15, g 15, h 5, i 5, j 10, k 4, l 3, m 4, n 20, o 5,
p 2, q 2/;
alias (activity, x, y);
set prec(x,y) "Precedence Order"
/A.(B,C,D)
(B,C,D).E
E.(F,G,H)
F.N
G.(K,I)
H.I
K.M
I.J
J.L
L.M
M.N
N.O
O.P
P.Q/ ;
Free Variable
T Completion Time;
Nonnegative Variables
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q;
equations
t_A, t_B, t_C, t_D, t_E, t_F, t_G, t_H, t_I, t_J, t_K, t_L, t_M, t_N, t_O, t_P,
t_Q, s_AB, s_AC, s_AD, s_BE, s_CE, s_DE, s_EF, s_EG, s_EH, s_FN, s_GK, s_GI,
s_HI, s_KM, s_IJ, s_JL, s_LM, s_MN, s_NO, s_OP, s_PQ;
t_A.. T =G= A + 15;
t_B.. T =G= B + 4;
t_C.. T =G= C + 5;
t_D.. T =G= D + 10;
t_E.. T =G= E + 4;
t_F.. T =G= F + 15;
t_G.. T =G= G + 15;
t_H.. T =G= H + 5;
t_I.. T =G= I + 5;
t_J.. T =G= J + 10;
t_K.. T =G= K + 4;
t_L.. T =G= L + 3;
t_M.. T =G= M + 4;
t_N.. T =G= N + 20;
t_O.. T =G= O + 5;
t_P.. T =G= P + 2;
t_Q.. T =G= Q + 10;
s_AB.. A + 15 =L= B;
s_AC.. A + 15 =L= C;
s_AD.. A + 15 =L= D;
s_BE.. B + 4 =L= E;
s_CE.. C + 5 =L= E;
s_DE.. D + 10 =L= E;
s_EF.. E + 4 =L= F;
s_EG.. E + 4 =L= G;
s_EH.. E + 4 =L= H;
s_FN.. F + 15 =L= N;
s_GK.. G + 15 =L= K;
s_GI.. G + 15 =L= I;
s_HI.. H + 5 =L= I;
s_KM.. K + 4 =L= M;
s_IJ.. I + 5 =L= J;
s_JL.. J + 10 =L= L;
s_LM.. L + 3 =L= M;
s_MN.. M + 4 =L= N;
s_NO.. N + 20 =L= O;
s_OP.. O + 5 =L= P;
s_PQ.. P + 2 =L= Q;
model thesis /all/;
solve thesis using lp minimizing t;
display t.l;
This code gives me a ton of details, such as what days I should start each activity. However, it doesn't tell me (as far as I can see), which activities are on the critical path. How can I determine this using GAMS?
A dependency lies on the critical path if the marginal on the dependency equation (s_AB etc. in your formulation) is -1. You can check this in the listing (.lst) file or by using the code below:
set activity /a*q/;
parameter duration (activity) "in days"
/a 15, b 4, c 5, d 10, e 4, f 15, g 15, h 5, i 5, j 10, k 4, l 3, m 4, n 20, o 5,
p 2, q 2/;
alias (activity, x, y);
set prec(x,y) "dependency Order"
/A.(B,C,D)
(B,C,D).E
E.(F,G,H)
F.N
G.(K,I)
H.I
K.M
I.J
J.L
L.M
M.N
N.O
O.P
P.Q/;
Free Variable
T Completion Time;
Nonnegative Variables
start(x);
equations
t_bound(x)
dependency(x,y);
t_bound(x).. T =G= start(x) + duration(x);
dependency(x,y)$prec(x,y).. start(x) + duration(x) =l= start(y);
model thesis /all/;
solve thesis using lp minimizing t;
set criticalpath(x,y);
criticalpath(x,y) = 1$(dependency.M(x,y) < 0);
display t.l, criticalpath;
I'm trying to create a program which can automatically solve word search puzzles.
I'm having some trouble storing the inputted word search puzzle. The example word search text file I'm supplied with contains letters separated by two spaces. There is a blank line between each populated line.
This is the code that I have right now. It seems to only store and output the left most column of the puzzle. What am I doing incorrectly? Is there a cleaner way of doing this?
vector< vector <string> > puzzle;
ifstream myPuzzle ("puzzle.txt");
if (myPuzzle.is_open())
{
while (myPuzzle)
{
string temp;
if (!getline(myPuzzle, temp))
break;
istringstream ss(temp);
vector <string> record;
while (ss)
{
string temp2;
if (!getline(ss, temp2, ' '))
break;
record.push_back(temp2);
}
puzzle.push_back(record);
}
}
else
cout << "Failed to open puzzle file!" << endl;
cout << "Puzzle succesfully inputted" << endl;
for (vector< vector<string> >::size_type i = 0; i < puzzle.size(); ++i)
{
for (vector<string>::size_type it = 0; it < puzzle[i].size(); i++)
cout << puzzle[i][it];
cout << endl;
}
Example matrix of characters (puzzle.txt):
U T Y Y A L P S G I B S K I P Q H S K T
A T L T I R U N E Z I M O D N A R S F P
D J I P R H H V E U E W U K F D L T A V
R T N Q N O E Y U O L H D S P Q C A F H
A A E U O O S A J F B O I Z S C C C V L
T T A I D P I B P L B L C E T H P K T I
I I R C U P Q S A W U B T Y N P E R T Y
C S T K U M S C R O B I I K N I L L H R
D E N B N A K I R U O N O H L I Q Y L A
F L A M I Z M M E S C S N S B B H O H N
U E T W O F O H D E M E A G E T E S X I
R C S X N I V T A I E R R Z Y G K V M B
C T N F I V C I G O R T Y X E H X G W D
D I O T W D Y R O I G I Z L V L P V Q N
J O C S E W L A U R E O U Q W O S A T I
I N O I U M F G B D L N C C Y O N M R F
L K P L E D U O I A M O R T I Z E J B G
M S Y Y U R V L Q D F P T E E R T K C W
You use i++ in both for loops. The second one should use it++.