Вы находитесь на странице: 1из 3

CS 3114 HW04

Duke Forsyth(dukef)

1)
0
1
2
3
4
5
6
7
8
9 10 Current solution set
---------------------------------------------------------------------------0
add
6
x
x
4
x
x
5
x
x
x
{0}
5
x
x add
7
x
5
x
x
x
{0, 4}
add
8
x
6
x
5
x
x
x
{0, 4, 1}
8
x
6
x add 10
x
x
{0, 4, 1, 7}
7 12
add 13
8
9
x
{0, 4, 1, 7, 5}
add
9
13
8
9
x
{0, 4, 1, 7, 5, 2}
9
13
add
9
x
{0, 4, 1, 7, 5, 2, 8}
add
11
9 11
{0, 4, 1, 7, 5, 2, 8, 3
}
10
add 11
{0, 4, 1, 7, 5, 2, 8, 3
, 9}
add
{0, 4, 1, 7, 5, 2, 8, 3, 9, 6}
add
{0, 4, 1, 7, 5, 2, 8, 3, 9, 6, 10}
---------------------------------------------------------------------------Solution:
Node
0
1
2
3
4
5
6
7
8
9 10
---------------------------------------------------------------------------Distance
0
5
7
9
4
6 10
5
8
9 11
---------------------------------------------------------------------------2)
The following is a tabular presentation of the topological ordering algorithm
trace from the Topo Ordering notes (using depth-first traversal):
Start at: 0
Pass 1: 0,1,2,7,8,9,6,5,4,3,10
Solution: 0,1,2,7,8,9,6,5,4,3,10
3)
a)Yes it does help.
step 1)create a hashtable that can contain all of the strings in quest
ion
step 2)before insertion check if the strings reverse already exists in
the table (if true step 3 if false step 4)
step 3)if step 2 returns true then we have at least one mirror set of
strings so done and exit
step 4)if step 2 returns false insert this string into the hashtable a
nd repeat step 2
b)Yes it does help.
step 1)create array with the set of positive integers in asceding orde
r.
step 2)first run sum the 1st and 2nd index add together and search fro
m the other indexes note exclude searching
those indexes in the summation

step 3)should the summation check in step 2 return true from any then
done and exit returning true
step 4)should the first round of searches fail increment the 2nd eleme
nt until the end of the array
then at the end of the 1st element iteration move to the 2nd and 3r
d element and ignore the first element
an example would be 1,5,4,2,3 => 1,2,3,4,5 with the firs
t pass youd find that 1+2 =3 which is the next element or first spot to search.
c)Yes it does help.
If you sort the strings by the length of each string that would
cut the cases
where the sub-string is longer then the string it is being compa
red to.
Other then that you can't do much aside from that you have to ch
eck the entire string against the whole substring.
d)TYPO(sting => string) <= extra credit? :P
Yes sorting helps.
For each string, if the set of strings is sorted By the the lettering
then any string that has
S as a prefix will be near to S. Otherwise, we have to search every strin
g in the set.
e)Yes it does.
If you allot an array of the biggest value in the set or in the case neg
atives are allowed the distance from minimum to the maximum number that is in th
e set
and map all the numbers in the set to a corresponding slot such as 1->1,
103->103 and map the numbers in the set to their corresponding slot should nega
tives
be in the set create a array that contains the smallest to the largest v
alue then map the 0th slot to the the smallest value and climb by 1 to the maxim
um
value and insert each element as it should be.
Then with the given number find its multiples and then compare its multi
ples to the slots until reaching the maximum value in the array.
thus you would only search the multiples so 2 would search 4,6,8,10 and
3 would search 3,6,9 etc etc cutting the comparisons
down considerably should the set be very seizable or having strange sear
ch requests. Mind you this is very impratical to program.

4)Side Note but isn't an array set as (int[] A) not (int A[]) <= extra credit? :
P
void sortBits(int A[])
{
int left = 0;
int right = A[].length -1;
while(left < right)
{
while(A[left] ==0 && left< right)
{

left++;
}
while(A[right] == 1 && left<right)
{
right--;
}
if(left < right)
{
A[left] = 0;
A[right] = 1;
left++;
right++;
}
}
}

Вам также может понравиться