Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > sci.math > #325249 > unrolled thread
| Started by | Paul <pepstein5@gmail.com> |
|---|---|
| First post | 2015-12-30 09:41 -0800 |
| Last post | 2016-01-03 04:55 +0000 |
| Articles | 15 — 6 participants |
Back to article view | Back to sci.math
Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2015-12-30 09:41 -0800
Re: Algorithm involving binary strings. Virgil <VIRGIL@VIRGIL.com> - 2015-12-30 13:16 -0700
Re: Algorithm involving binary strings. quasi <quasi@null.set> - 2015-12-30 16:30 -0500
Re: Algorithm involving binary strings. James Waldby <not@valid.invalid> - 2015-12-31 01:18 +0000
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2015-12-31 06:53 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2015-12-31 06:46 -0800
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2015-12-31 07:38 -0800
Re: Algorithm involving binary strings. John Forkosh <forkosh@panix.com> - 2016-01-01 02:19 +0000
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-01 11:44 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2016-01-01 05:00 -0800
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-02 02:11 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2016-01-02 02:20 -0800
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-02 12:50 +0000
Re: Algorithm involving binary strings. Paul <pepstein5@gmail.com> - 2016-01-02 07:12 -0800
Re: Algorithm involving binary strings. JohnF <john@please.see.sig.for.email.com> - 2016-01-03 04:55 +0000
| From | Paul <pepstein5@gmail.com> |
|---|---|
| Date | 2015-12-30 09:41 -0800 |
| Subject | Algorithm involving binary strings. |
| Message-ID | <4f1a0e21-4563-4697-8b6e-b4c41f8381ae@googlegroups.com> |
Suppose we are given a collection of binary strings all of the same length. The problem is to find the largest number n such that there exist distinct strings s1, s2 such that n positions are set to 1 in both s1 and s2 simultaneously. Also, the task is to find all pairs so that this maximum is realised. This is easy to do but difficult to do efficiently. Could someone help me, perhaps by referencing the right techniques to look at? No, this isn't a homework problem. No, I'm not enrolled in any courses. I'm trying to learn some computer science. Thanks, Paul
[toc] | [next] | [standalone]
| From | Virgil <VIRGIL@VIRGIL.com> |
|---|---|
| Date | 2015-12-30 13:16 -0700 |
| Message-ID | <VIRGIL-F2D82C.13165230122015@news.giganews.com> |
| In reply to | #325249 |
In article <4f1a0e21-4563-4697-8b6e-b4c41f8381ae@googlegroups.com>, Paul <pepstein5@gmail.com> wrote: > Suppose we are given a collection of binary strings all of the same length. > The problem is to find the largest number n such that there exist distinct > strings s1, s2 such that n positions are set to 1 in both s1 and s2 > simultaneously. Also, the task is to find all pairs so that this maximum is > realised. > > This is easy to do but difficult to do efficiently. Could someone help me, > perhaps by referencing the right techniques to look at? > > No, this isn't a homework problem. No, I'm not enrolled in any courses. I'm > trying to learn some computer science. > > Thanks, > > Paul Somewhere in this process you are going to have to count and record the number of 1's in each string, so you might as well start by doing so and recording those counts, after which it should be easy. -- Virgil "Mit der Dummheit kampfen Gotter selbst vergebens." (Schiller)
[toc] | [prev] | [next] | [standalone]
| From | quasi <quasi@null.set> |
|---|---|
| Date | 2015-12-30 16:30 -0500 |
| Message-ID | <shi88b53obck9bk6mgt63pe822jep4cv03@4ax.com> |
| In reply to | #325249 |
Paul wrote: >Suppose we are given a collection of binary strings all of >the same length. The problem is to find the largest number >n such that there exist distinct strings s1, s2 such that n >positions are set to 1 in both s1 and s2 simultaneously. >Also, the task is to find all pairs so that this maximum is >realised. Let S be a set of m binary strings, each of length k. For the purpose of this problem, are the numbers m,k potentially "huge"? >This is easy to do but difficult to do efficiently. In other words, you want an algorithm that's computationally efficient in an order of magnitude sense? In terms of m and k? In terms of m*k? What "easy" algorithm do you already have? What's its order of magnitude cost of computation? quasi
[toc] | [prev] | [next] | [standalone]
| From | James Waldby <not@valid.invalid> |
|---|---|
| Date | 2015-12-31 01:18 +0000 |
| Message-ID | <n61vom$4fj$1@dont-email.me> |
| In reply to | #325265 |
On Wed, 30 Dec 2015 16:30:04 -0500, quasi wrote: > Paul wrote: > >>Suppose we are given a collection of binary strings all of >>the same length. The problem is to find the largest number >>n such that there exist distinct strings s1, s2 such that n >>positions are set to 1 in both s1 and s2 simultaneously. >>Also, the task is to find all pairs so that this maximum is >>realised. > > Let S be a set of m binary strings, each of length k. > > For the purpose of this problem, are the numbers m,k > potentially "huge"? Another question is if 1-bits are sparse. For example, there could be some heuristic method that does well, if some certain proportion of 1-bits facilitates divide-and-conquer. >>This is easy to do but difficult to do efficiently. > > In other words, you want an algorithm that's computationally > efficient in an order of magnitude sense? In terms of m and > k? In terms of m*k? > > What "easy" algorithm do you already have? What's its order > of magnitude cost of computation? The most-obvious algorithm -- testing all pairs of strings -- costs O(k*m^2). Perhaps some alpha-beta pruning search method, using a string's bit population (number of 1s) and its balance (average location of 1s) or other measures, could on average lower the number of comparisons made (but would probably still have O(k*m^2) worst case cost). -- jiw
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2015-12-31 06:53 +0000 |
| Message-ID | <n62jd3$kmf$1@reader1.panix.com> |
| In reply to | #325249 |
Paul <pepstein5@gmail.com> wrote: > Suppose we are given a collection of binary strings all of > the same length. The problem is to find the largest number n > such that there exist distinct strings s1, s2 such that n positions > are set to 1 in both s1 and s2 simultaneously. Also, the task > is to find all pairs so that this maximum is realised. > > This is easy to do but difficult to do efficiently. Could someone > help me, perhaps by referencing the right techniques to look at? > > No, this isn't a homework problem. No, I'm not enrolled in any > courses. I'm trying to learn some computer science. > Thanks, Paul (a) just curious: what suggested this problem to you? To me, as far as I can tell, it seems pretty useless, application-wise. And if you're "trying to learn some computer science", one of the first things to learn is recognizing useful or theoretically important problems. And, unless I'm wrong, this ain't one. But please correct me if I'm wrong about that -- then I'll learn something:). (b) there's efficient wrt computational complexity, and efficient wrt making efficient use of hardware architecture. I can't say about the first, but you can make use of 8-bit byte addressability of virtually all architectures, as follows. First, if n isn't a multiple of 8, concat some 0 bits to each string so that it is. Now munge through each string byte-by-byte. And have a 256-int table with nOnes[i]=the number of 1's for value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8. Then just sum up nOnes for each byte of a string. That'll reduce your loop by a factor of 8. (And I leave you to answer why munging through your string 32-bit-int by 32-bit-int isn't optimal.) And once you know the number of 1's in each string, the rest is easy ... except for uniqueness. If your collection of strings may contain duplicates, you'll also want to accumulate some md5-like hash of each string as you munge through them. (c) you just talking about learning, or you going to show us some code? Here's the function prototype in C, int nones ( unsigned char *s, int len ) /* s = ptr to bit string (beginning on byte boundary) */ /* len = total #bits in s */ /* returns #bits in s that are 1's */ Please follow up with that function... -- John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | Paul <pepstein5@gmail.com> |
|---|---|
| Date | 2015-12-31 06:46 -0800 |
| Message-ID | <b5cc51c7-94b1-469b-ac7c-2276f4bc4633@googlegroups.com> |
| In reply to | #325283 |
On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
> Paul <pepstein5@gmail.com> wrote:
> > Suppose we are given a collection of binary strings all of
> > the same length. The problem is to find the largest number n
> > such that there exist distinct strings s1, s2 such that n positions
> > are set to 1 in both s1 and s2 simultaneously. Also, the task
> > is to find all pairs so that this maximum is realised.
> >
> > This is easy to do but difficult to do efficiently. Could someone
> > help me, perhaps by referencing the right techniques to look at?
> >
> > No, this isn't a homework problem. No, I'm not enrolled in any
> > courses. I'm trying to learn some computer science.
> > Thanks, Paul
>
> (a) just curious: what suggested this problem to you?
> To me, as far as I can tell, it seems pretty useless,
> application-wise. And if you're "trying to learn some
> computer science", one of the first things to learn is
> recognizing useful or theoretically important problems.
> And, unless I'm wrong, this ain't one. But please correct
> me if I'm wrong about that -- then I'll learn something:).
>
> (b) there's efficient wrt computational complexity, and
> efficient wrt making efficient use of hardware architecture.
> I can't say about the first, but you can make use of 8-bit
> byte addressability of virtually all architectures, as follows.
> First, if n isn't a multiple of 8, concat some 0 bits to each
> string so that it is. Now munge through each string byte-by-byte.
> And have a 256-int table with nOnes[i]=the number of 1's for
> value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
> Then just sum up nOnes for each byte of a string. That'll reduce
> your loop by a factor of 8. (And I leave you to answer why munging
> through your string 32-bit-int by 32-bit-int isn't optimal.)
> And once you know the number of 1's in each string, the
> rest is easy ... except for uniqueness. If your collection
> of strings may contain duplicates, you'll also want to
> accumulate some md5-like hash of each string as you munge
> through them.
>
> (c) you just talking about learning, or you going to
> show us some code? Here's the function prototype in C,
> int nones ( unsigned char *s, int len )
> /* s = ptr to bit string (beginning on byte boundary) */
> /* len = total #bits in s */
> /* returns #bits in s that are 1's */
> Please follow up with that function...
Thanks to everyone for their comments. I'm doing challenge problems on hackerrank to prepare for a software dev interview. Unfortunately, I misunderstood the problem. I did solve it correctly (according to hackerrank) but I'm concerned about inefficiency. Here is the link: https://www.hackerrank.com/challenges/acm-icpc-team However, since it's https that may mean that passwords etc. are necessary. Here then is a copy-paste of the question (note that the question is different to what I said here previously.) Many thanks for your feedback.
Paul
********* BEGIN PROBLEM STATEMENT
Problem Statement
You are given a list of N people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. Find out the maximum number of topics a 2-person team can know. And also find out how many teams can know that maximum number of topics.
Note Suppose a, b, and c are three different people, then (a,b) and (b,c) are counted as two different teams.
Input Format
The first line contains two integers, N and M, separated by a single space, where N represents the number of people, and M represents the number of topics. N lines follow.
Each line contains a binary string of length M. If the ith line's jth character is 1, then the ith person knows the jth topic; otherwise, he doesn't know the topic.
Constraints
2≤N≤500
1≤M≤500
Output Format
On the first line, print the maximum number of topics a 2-person team can know.
On the second line, print the number of 2-person teams that can know the maximum number of topics.
Sample Input
4 5
10101
11100
11010
00101
Sample Output
5
2
Explanation
(1, 3) and (3, 4) know all the 5 topics. So the maximal topics a 2-person team knows is 5, and only 2 teams can achieve this.
***** END PROBLEM STATEMENT.
Here is my solution with the #includes omitted.
int main(){
int n;
int m;
cin >> n >> m;
vector<string> topic(n);
for(int topic_i = 0;topic_i < n;topic_i++){
cin >> topic[topic_i];
}
int numTopics = 0;
int knowers = 0;
for(int i = 0; i < n; ++i)
for(int j = i + 1; j < n; ++j)
{
int knownByPair = 0;
for(int k = 0; k < m; ++k)
{
if(topic[i][k] == '1' || topic[j][k] == '1')
++knownByPair;
}
if(knownByPair > numTopics )
{
knowers = 1;
numTopics = knownByPair;
}
else if(knownByPair == numTopics)
++knowers;
}
cout << numTopics << endl << knowers;
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Paul <pepstein5@gmail.com> |
|---|---|
| Date | 2015-12-31 07:38 -0800 |
| Message-ID | <0685fc25-f598-47c8-b037-23ac17659e10@googlegroups.com> |
| In reply to | #325293 |
On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote:
> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
> > Paul <pepstein5@gmail.com> wrote:
> > > Suppose we are given a collection of binary strings all of
> > > the same length. The problem is to find the largest number n
> > > such that there exist distinct strings s1, s2 such that n positions
> > > are set to 1 in both s1 and s2 simultaneously. Also, the task
> > > is to find all pairs so that this maximum is realised.
> > >
> > > This is easy to do but difficult to do efficiently. Could someone
> > > help me, perhaps by referencing the right techniques to look at?
> > >
> > > No, this isn't a homework problem. No, I'm not enrolled in any
> > > courses. I'm trying to learn some computer science.
> > > Thanks, Paul
> >
> > (a) just curious: what suggested this problem to you?
> > To me, as far as I can tell, it seems pretty useless,
> > application-wise. And if you're "trying to learn some
> > computer science", one of the first things to learn is
> > recognizing useful or theoretically important problems.
> > And, unless I'm wrong, this ain't one. But please correct
> > me if I'm wrong about that -- then I'll learn something:).
> >
> > (b) there's efficient wrt computational complexity, and
> > efficient wrt making efficient use of hardware architecture.
> > I can't say about the first, but you can make use of 8-bit
> > byte addressability of virtually all architectures, as follows.
> > First, if n isn't a multiple of 8, concat some 0 bits to each
> > string so that it is. Now munge through each string byte-by-byte.
> > And have a 256-int table with nOnes[i]=the number of 1's for
> > value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
> > Then just sum up nOnes for each byte of a string. That'll reduce
> > your loop by a factor of 8. (And I leave you to answer why munging
> > through your string 32-bit-int by 32-bit-int isn't optimal.)
> > And once you know the number of 1's in each string, the
> > rest is easy ... except for uniqueness. If your collection
> > of strings may contain duplicates, you'll also want to
> > accumulate some md5-like hash of each string as you munge
> > through them.
> >
> > (c) you just talking about learning, or you going to
> > show us some code? Here's the function prototype in C,
> > int nones ( unsigned char *s, int len )
> > /* s = ptr to bit string (beginning on byte boundary) */
> > /* len = total #bits in s */
> > /* returns #bits in s that are 1's */
> > Please follow up with that function...
>
> Thanks to everyone for their comments. I'm doing challenge problems on hackerrank to prepare for a software dev interview. Unfortunately, I misunderstood the problem. I did solve it correctly (according to hackerrank) but I'm concerned about inefficiency. Here is the link: https://www.hackerrank.com/challenges/acm-icpc-team However, since it's https that may mean that passwords etc. are necessary. Here then is a copy-paste of the question (note that the question is different to what I said here previously.) Many thanks for your feedback.
>
> Paul
>
> ********* BEGIN PROBLEM STATEMENT
> Problem Statement
>
> You are given a list of N people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. Find out the maximum number of topics a 2-person team can know. And also find out how many teams can know that maximum number of topics.
>
> Note Suppose a, b, and c are three different people, then (a,b) and (b,c) are counted as two different teams.
>
> Input Format
>
> The first line contains two integers, N and M, separated by a single space, where N represents the number of people, and M represents the number of topics. N lines follow.
> Each line contains a binary string of length M. If the ith line's jth character is 1, then the ith person knows the jth topic; otherwise, he doesn't know the topic.
>
> Constraints
> 2≤N≤500
> 1≤M≤500
>
> Output Format
>
> On the first line, print the maximum number of topics a 2-person team can know.
> On the second line, print the number of 2-person teams that can know the maximum number of topics.
>
> Sample Input
>
> 4 5
> 10101
> 11100
> 11010
> 00101
> Sample Output
>
> 5
> 2
> Explanation
>
> (1, 3) and (3, 4) know all the 5 topics. So the maximal topics a 2-person team knows is 5, and only 2 teams can achieve this.
> ***** END PROBLEM STATEMENT.
>
> Here is my solution with the #includes omitted.
> int main(){
> int n;
> int m;
> cin >> n >> m;
> vector<string> topic(n);
> for(int topic_i = 0;topic_i < n;topic_i++){
> cin >> topic[topic_i];
> }
> int numTopics = 0;
> int knowers = 0;
>
> for(int i = 0; i < n; ++i)
> for(int j = i + 1; j < n; ++j)
> {
> int knownByPair = 0;
> for(int k = 0; k < m; ++k)
> {
> if(topic[i][k] == '1' || topic[j][k] == '1')
> ++knownByPair;
> }
>
> if(knownByPair > numTopics )
> {
> knowers = 1;
> numTopics = knownByPair;
> }
> else if(knownByPair == numTopics)
> ++knowers;
>
> }
> cout << numTopics << endl << knowers;
> return 0;
> }
I've spotted an added efficiency myself.
for(int k = 0; k < m; ++k)
{
if(topic[i][k] == '1' || topic[j][k] == '1')
++knownByPair;
}
We don't actually need to go all the way to m if we already know that knownByPair will be less than numTopics.
Hence
for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k)
{
// body unchanged.
}
This also passes the hackerrank test but doesn't feel like a very substantial improvement.
Paul
[toc] | [prev] | [next] | [standalone]
| From | John Forkosh <forkosh@panix.com> |
|---|---|
| Date | 2016-01-01 02:19 +0000 |
| Message-ID | <n64nmv$lrv$1@reader1.panix.com> |
| In reply to | #325295 |
Excuse the top-posting, but just a few comments...
(a) right about your remark that you originally mis-stated the problem.
The problem statement clarifies what you wanted.
(b) here's a possibly better approach than the "brute force" approach
you have below, though the 500-topic max makes it a bit messier than
it might be: what you want is an (inclusive) "or" of all pairs of
bit strings. Each pair's a putative team. Once you've "or"'ed a team's
two bit strings, then just count the number of 1's in the "or", like
we all originally discussed. And the rest is just simple house-keeping.
If you can "or" a 64-bit unsigned long long, then that lets you
simultaneously (with one operation) check 64 topics. So just
declare unsigned long long people[500][8];, where 8*64=512
accommodates the 500 max topics. Then parse and interpret those
input strings accordingly during initialization. That'll be a
little harder than what you have below, but then the real work
should be a lot quicker.
(c) if you want to self-study algorithms, I think I can safely say
one of the best books is Cormen, et al's (long line follows),
http://bayanbox.ir/view/4177858657730907268/introduction-to-algorithms-3rd-edition.pdf
I'm surprised it's downloadable for free. You can just wget the whole thing.
Paul <pepstein5@gmail.com> wrote:
> On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote:
>> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
>> > Paul <pepstein5@gmail.com> wrote:
>> > > Suppose we are given a collection of binary strings all of
>> > > the same length. The problem is to find the largest number n
>> > > such that there exist distinct strings s1, s2 such that n positions
>> > > are set to 1 in both s1 and s2 simultaneously. Also, the task
>> > > is to find all pairs so that this maximum is realised.
>> > >
>> > > This is easy to do but difficult to do efficiently. Could someone
>> > > help me, perhaps by referencing the right techniques to look at?
>> > >
>> > > No, this isn't a homework problem. No, I'm not enrolled in any
>> > > courses. I'm trying to learn some computer science.
>> > > Thanks, Paul
>> >
>> > (a) just curious: what suggested this problem to you?
>> > To me, as far as I can tell, it seems pretty useless,
>> > application-wise. And if you're "trying to learn some
>> > computer science", one of the first things to learn is
>> > recognizing useful or theoretically important problems.
>> > And, unless I'm wrong, this ain't one. But please correct
>> > me if I'm wrong about that -- then I'll learn something:).
>> >
>> > (b) there's efficient wrt computational complexity, and
>> > efficient wrt making efficient use of hardware architecture.
>> > I can't say about the first, but you can make use of 8-bit
>> > byte addressability of virtually all architectures, as follows.
>> > First, if n isn't a multiple of 8, concat some 0 bits to each
>> > string so that it is. Now munge through each string byte-by-byte.
>> > And have a 256-int table with nOnes[i]=the number of 1's for
>> > value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
>> > Then just sum up nOnes for each byte of a string. That'll reduce
>> > your loop by a factor of 8. (And I leave you to answer why munging
>> > through your string 32-bit-int by 32-bit-int isn't optimal.)
>> > And once you know the number of 1's in each string, the
>> > rest is easy ... except for uniqueness. If your collection
>> > of strings may contain duplicates, you'll also want to
>> > accumulate some md5-like hash of each string as you munge
>> > through them.
>> >
>> > (c) you just talking about learning, or you going to
>> > show us some code? Here's the function prototype in C,
>> > int nones ( unsigned char *s, int len )
>> > /* s = ptr to bit string (beginning on byte boundary) */
>> > /* len = total #bits in s */
>> > /* returns #bits in s that are 1's */
>> > Please follow up with that function...
>>
> Thanks to everyone for their comments. I'm doing challenge
> problems on hackerrank to prepare for a software dev interview.
> Unfortunately, I misunderstood the problem. I did solve it
> correctly (according to hackerrank) but I'm concerned about
> inefficiency. Here is the link:
> https://www.hackerrank.com/challenges/acm-icpc-team
> However, since it's https that may mean that passwords etc.
> are necessary. Here then is a copy-paste of the question (note
> that the question is different to what I said here previously.)
> Many thanks for your feedback.
> Paul
>
> ********* BEGIN PROBLEM STATEMENT
> Problem Statement
>
> You are given a list of N people who are attending ACM-ICPC
> World Finals. Each of them are either well versed in a topic
> or they are not. Find out the maximum number of topics
> a 2-person team can know. And also find out how many teams
> can know that maximum number of topics.
>
> Note Suppose a, b, and c are three different people,
> then (a,b) and (b,c) are counted as two different teams.
>
> Input Format
> The first line contains two integers, N and M, separated
> by a single space, where N represents the number of people,
> and M represents the number of topics. N lines follow.
> Each line contains a binary string of length M. If the ith line's
> jth character is 1, then the ith person knows the jth topic;
> otherwise, he doesn't know the topic.
>
> Constraints
> 2?N?500
> 1?M?500
>
> Output Format
> On the first line, print the maximum number of topics a
> 2-person team can know. On the second line, print the number
> of 2-person teams that can know the maximum number of topics.
>
> Sample Input
> 4 5
> 10101
> 11100
> 11010
> 00101
>
> Sample Output
> 5
> 2
>
> Explanation
> (1, 3) and (3, 4) know all the 5 topics. So the maximal topics
> a 2-person team knows is 5, and only 2 teams can achieve this.
> ***** END PROBLEM STATEMENT.
>
> Here is my solution with the #includes omitted.
> int main() {
> int n;
> int m;
> cin >> n >> m;
> vector<string> topic(n);
> for(int topic_i = 0;topic_i < n;topic_i++) {
> cin >> topic[topic_i]; }
> int numTopics = 0;
> int knowers = 0;
>
> for(int i = 0; i < n; ++i)
> for(int j = i + 1; j < n; ++j) {
> int knownByPair = 0;
> for(int k = 0; k < m; ++k) {
> if(topic[i][k] == '1' || topic[j][k] == '1')
> ++knownByPair; }
> if(knownByPair > numTopics ) {
> knowers = 1;
> numTopics = knownByPair; }
> else if(knownByPair == numTopics)
> ++knowers;
> }
> cout << numTopics << endl << knowers;
> return 0;
> }
>
> I've spotted an added efficiency myself.
> for(int k = 0; k < m; ++k) {
> if(topic[i][k] == '1' || topic[j][k] == '1')
> ++knownByPair; }
> We don't actually need to go all the way to m if we already
> know that knownByPair will be less than numTopics.
> Hence
> for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k) {
> // body unchanged. }
> This also passes the hackerrank test but doesn't feel
> like a very substantial improvement.
> Paul
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2016-01-01 11:44 +0000 |
| Message-ID | <n65ori$lg4$1@reader1.panix.com> |
| In reply to | #325309 |
This time, excuse both the top-posting and following myself up...
I note the upper-right corner of that page you cited,
https://www.hackerrank.com/challenges/acm-icpc-team
says "Related Topics: Finding Max Min Bitwise OR",
so they're clearly hinting at the approach I suggested
below (honest, I didn't notice that till after posting
that earlier follow-up:)
But maybe you can get some "extra credit" be generalizing, e.g.,
(a) less interesting: no reason why teams consist of two people.
Let N=#people, M=#topics as stated, and generalize your code
for K=#people_on_a_team, default K=2. See how answers behave,
e.g., obviously both output numbers monotonically increase with K,
but faster/slower than linear, or what? So do all K=2,...,N
(actually, I guess you can start with K=1). And then,
instead of the original problem statement's sample output
5
2
Your sample output should be
K=2 K=3 ... K=N
5 etc
2
(b) way more interesting, to me at least, and I'd guess for
sci.math applicability:
For each person, fill their M-bit string randomly, with
probability 0<=p<=1 of a "1" in each position. Now, let
N=500 (or even larger) and plot a pdf (probability distribution
function) of x-axis=#topics_a_team_knows=0-to-M, and
y-axis=#teams_that_know_that_#topics (and make it a pdf by
normalizing, i.e., if #people=N then #teams=N^2-N when K=2, so
divide y-value by that, whereby sum_(i=#topics=0-to-M)y_i = 1.0)
So this pdf will be a function of all your N,M,K,p, I think
(maybe not N so much, except to get a good sample size).
And it doesn't seem so ad hoc that there'd be no closed-form
solution. But I don't offhand see what that closed-form is
(for p=0,1, I think we know the answer immediately).
Anyway, I think these generalizations might "spice up" the
original problem statement a bit, especially the second one.
And try thinking up a third generalization (,fourth, etc).
JohnF wrote:
> Excuse the top-posting, but just a few comments...
> (a) right about your remark that you originally mis-stated the problem.
> The problem statement below clarifies what you wanted.
> (b) here's a possibly better approach than the "brute force" approach
> you have below, though the 500-topic max makes it a bit messier than
> it might be: what you want is an (inclusive) "or" of all pairs of
> bit strings. Each pair's a putative team. Once you've "or"'ed a team's
> two bit strings, then just count the number of 1's in the "or", like
> we all originally discussed. And the rest is just simple house-keeping.
> If you can "or" a 64-bit unsigned long long, then that lets you
> simultaneously (with one operation) check 64 topics. So just
> declare unsigned long long people[500][8];, where 8*64=512
> accommodates the 500 max topics. Then parse and interpret those
> input strings accordingly during initialization. That'll be a
> little harder than what you have below, but then the real work
> should be a lot quicker.
> (c) if you want to self-study algorithms, I think I can safely say
> one of the best books is Cormen, et al's (long line follows),
> http://bayanbox.ir/view/4177858657730907268/introduction-to-algorithms-3rd-edition.pdf
> I'm surprised it's downloadable for free. You can just wget the whole thing.
>
> Paul <pepstein5@gmail.com> wrote:
>> On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote:
>>> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
>>> > Paul <pepstein5@gmail.com> wrote:
>>> > > Suppose we are given a collection of binary strings all of
>>> > > the same length. The problem is to find the largest number n
>>> > > such that there exist distinct strings s1, s2 such that n positions
>>> > > are set to 1 in both s1 and s2 simultaneously. Also, the task
>>> > > is to find all pairs so that this maximum is realised.
>>> > >
>>> > > This is easy to do but difficult to do efficiently. Could someone
>>> > > help me, perhaps by referencing the right techniques to look at?
>>> > >
>>> > > No, this isn't a homework problem. No, I'm not enrolled in any
>>> > > courses. I'm trying to learn some computer science.
>>> > > Thanks, Paul
>>> >
>>> > (a) just curious: what suggested this problem to you?
>>> > To me, as far as I can tell, it seems pretty useless,
>>> > application-wise. And if you're "trying to learn some
>>> > computer science", one of the first things to learn is
>>> > recognizing useful or theoretically important problems.
>>> > And, unless I'm wrong, this ain't one. But please correct
>>> > me if I'm wrong about that -- then I'll learn something:).
>>> >
>>> > (b) there's efficient wrt computational complexity, and
>>> > efficient wrt making efficient use of hardware architecture.
>>> > I can't say about the first, but you can make use of 8-bit
>>> > byte addressability of virtually all architectures, as follows.
>>> > First, if n isn't a multiple of 8, concat some 0 bits to each
>>> > string so that it is. Now munge through each string byte-by-byte.
>>> > And have a 256-int table with nOnes[i]=the number of 1's for
>>> > value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
>>> > Then just sum up nOnes for each byte of a string. That'll reduce
>>> > your loop by a factor of 8. (And I leave you to answer why munging
>>> > through your string 32-bit-int by 32-bit-int isn't optimal.)
>>> > And once you know the number of 1's in each string, the
>>> > rest is easy ... except for uniqueness. If your collection
>>> > of strings may contain duplicates, you'll also want to
>>> > accumulate some md5-like hash of each string as you munge
>>> > through them.
>>> >
>>> > (c) you just talking about learning, or you going to
>>> > show us some code? Here's the function prototype in C,
>>> > int nones ( unsigned char *s, int len )
>>> > /* s = ptr to bit string (beginning on byte boundary) */
>>> > /* len = total #bits in s */
>>> > /* returns #bits in s that are 1's */
>>> > Please follow up with that function...
>>>
>> Thanks to everyone for their comments. I'm doing challenge
>> problems on hackerrank to prepare for a software dev interview.
>> Unfortunately, I misunderstood the problem. I did solve it
>> correctly (according to hackerrank) but I'm concerned about
>> inefficiency. Here is the link:
>> https://www.hackerrank.com/challenges/acm-icpc-team
>> However, since it's https that may mean that passwords etc.
>> are necessary. Here then is a copy-paste of the question (note
>> that the question is different to what I said here previously.)
>> Many thanks for your feedback.
>> Paul
>>
>> ********* BEGIN PROBLEM STATEMENT
>> Problem Statement
>>
>> You are given a list of N people who are attending ACM-ICPC
>> World Finals. Each of them are either well versed in a topic
>> or they are not. Find out the maximum number of topics
>> a 2-person team can know. And also find out how many teams
>> can know that maximum number of topics.
>>
>> Note Suppose a, b, and c are three different people,
>> then (a,b) and (b,c) are counted as two different teams.
>>
>> Input Format
>> The first line contains two integers, N and M, separated
>> by a single space, where N represents the number of people,
>> and M represents the number of topics. N lines follow.
>> Each line contains a binary string of length M. If the ith line's
>> jth character is 1, then the ith person knows the jth topic;
>> otherwise, he doesn't know the topic.
>>
>> Constraints
>> 2<=N<=500
>> 1<=M<=500
>>
>> Output Format
>> On the first line, print the maximum number of topics a
>> 2-person team can know. On the second line, print the number
>> of 2-person teams that can know the maximum number of topics.
>>
>> Sample Input
>> 4 5
>> 10101
>> 11100
>> 11010
>> 00101
>>
>> Sample Output
>> 5
>> 2
>>
>> Explanation
>> (1, 3) and (3, 4) know all the 5 topics. So the maximal topics
>> a 2-person team knows is 5, and only 2 teams can achieve this.
>> ***** END PROBLEM STATEMENT.
>>
>> Here is my solution with the #includes omitted.
>> int main() {
>> int n;
>> int m;
>> cin >> n >> m;
>> vector<string> topic(n);
>> for(int topic_i = 0;topic_i < n;topic_i++) {
>> cin >> topic[topic_i]; }
>> int numTopics = 0;
>> int knowers = 0;
>>
>> for(int i = 0; i < n; ++i)
>> for(int j = i + 1; j < n; ++j) {
>> int knownByPair = 0;
>> for(int k = 0; k < m; ++k) {
>> if(topic[i][k] == '1' || topic[j][k] == '1')
>> ++knownByPair; }
>> if(knownByPair > numTopics ) {
>> knowers = 1;
>> numTopics = knownByPair; }
>> else if(knownByPair == numTopics)
>> ++knowers;
>> }
>> cout << numTopics << endl << knowers;
>> return 0;
>> }
>>
>> I've spotted an added efficiency myself.
>> for(int k = 0; k < m; ++k) {
>> if(topic[i][k] == '1' || topic[j][k] == '1')
>> ++knownByPair; }
>> We don't actually need to go all the way to m if we already
>> know that knownByPair will be less than numTopics.
>> Hence
>> for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k) {
>> // body unchanged. }
>> This also passes the hackerrank test but doesn't feel
>> like a very substantial improvement.
>> Paul
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | Paul <pepstein5@gmail.com> |
|---|---|
| Date | 2016-01-01 05:00 -0800 |
| Message-ID | <6e5fb971-139e-4155-936e-4a0f8e6191ab@googlegroups.com> |
| In reply to | #325309 |
Thanks, John (and thanks also for your suggestions below). Since we've started top-posting, we might as well continue with this convention. Bitwise-or is the right concept. However, the right tool for this is bitset. I tried to apply your advice but to improve it by using bitset instead of several unsigned longs. The for loop now reads:
for(int i = 0; i < n; ++i)
for(int j = i + 1; j < n; ++j)
{
int knownByPair = 0;
bitset<500> topicRep = bitset<500>(topic[i]) | bitset<500>(topic[j]);
for(int k = 0; k < m; ++k)
knownByPair += topicRep[k];
if(knownByPair > numTopics )
{
knowers = 1;
numTopics = knownByPair;
}
else if(knownByPair == numTopics)
++knowers;
}
This has been checked on hackerrank's platform and satisfies their tests. Is this what you have in mind, or are there still problems?
Thanks for your suggestion. I already have this book. However, I tend to read books from cover to cover (with the possible exception of some exercises) and I find the problem of finding chapters/pages to omit very difficult. So I'm not sure such a huge book is a way to optimise my progress. Accordingly, I'm reading Algorithms Unlocked instead.
Most famous books are available free online. However, getting non-pirated versions from virus-free websites is harder. I tried to research the website bayanbox.ir and couldn't find any info. Do you recommend this website more generally?
Thanks,
Paul
On Friday, January 1, 2016 at 2:19:21 AM UTC, John Forkosh wrote:
> Excuse the top-posting, but just a few comments...
> (a) right about your remark that you originally mis-stated the problem.
> The problem statement clarifies what you wanted.
> (b) here's a possibly better approach than the "brute force" approach
> you have below, though the 500-topic max makes it a bit messier than
> it might be: what you want is an (inclusive) "or" of all pairs of
> bit strings. Each pair's a putative team. Once you've "or"'ed a team's
> two bit strings, then just count the number of 1's in the "or", like
> we all originally discussed. And the rest is just simple house-keeping.
> If you can "or" a 64-bit unsigned long long, then that lets you
> simultaneously (with one operation) check 64 topics. So just
> declare unsigned long long people[500][8];, where 8*64=512
> accommodates the 500 max topics. Then parse and interpret those
> input strings accordingly during initialization. That'll be a
> little harder than what you have below, but then the real work
> should be a lot quicker.
> (c) if you want to self-study algorithms, I think I can safely say
> one of the best books is Cormen, et al's (long line follows),
> http://bayanbox.ir/view/4177858657730907268/introduction-to-algorithms-3rd-edition.pdf
> I'm surprised it's downloadable for free. You can just wget the whole thing.
>
> Paul <pepstein5@gmail.com> wrote:
> > On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote:
> >> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
> >> > Paul <pepstein5@gmail.com> wrote:
> >> > > Suppose we are given a collection of binary strings all of
> >> > > the same length. The problem is to find the largest number n
> >> > > such that there exist distinct strings s1, s2 such that n positions
> >> > > are set to 1 in both s1 and s2 simultaneously. Also, the task
> >> > > is to find all pairs so that this maximum is realised.
> >> > >
> >> > > This is easy to do but difficult to do efficiently. Could someone
> >> > > help me, perhaps by referencing the right techniques to look at?
> >> > >
> >> > > No, this isn't a homework problem. No, I'm not enrolled in any
> >> > > courses. I'm trying to learn some computer science.
> >> > > Thanks, Paul
> >> >
> >> > (a) just curious: what suggested this problem to you?
> >> > To me, as far as I can tell, it seems pretty useless,
> >> > application-wise. And if you're "trying to learn some
> >> > computer science", one of the first things to learn is
> >> > recognizing useful or theoretically important problems.
> >> > And, unless I'm wrong, this ain't one. But please correct
> >> > me if I'm wrong about that -- then I'll learn something:).
> >> >
> >> > (b) there's efficient wrt computational complexity, and
> >> > efficient wrt making efficient use of hardware architecture.
> >> > I can't say about the first, but you can make use of 8-bit
> >> > byte addressability of virtually all architectures, as follows.
> >> > First, if n isn't a multiple of 8, concat some 0 bits to each
> >> > string so that it is. Now munge through each string byte-by-byte.
> >> > And have a 256-int table with nOnes[i]=the number of 1's for
> >> > value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
> >> > Then just sum up nOnes for each byte of a string. That'll reduce
> >> > your loop by a factor of 8. (And I leave you to answer why munging
> >> > through your string 32-bit-int by 32-bit-int isn't optimal.)
> >> > And once you know the number of 1's in each string, the
> >> > rest is easy ... except for uniqueness. If your collection
> >> > of strings may contain duplicates, you'll also want to
> >> > accumulate some md5-like hash of each string as you munge
> >> > through them.
> >> >
> >> > (c) you just talking about learning, or you going to
> >> > show us some code? Here's the function prototype in C,
> >> > int nones ( unsigned char *s, int len )
> >> > /* s = ptr to bit string (beginning on byte boundary) */
> >> > /* len = total #bits in s */
> >> > /* returns #bits in s that are 1's */
> >> > Please follow up with that function...
> >>
> > Thanks to everyone for their comments. I'm doing challenge
> > problems on hackerrank to prepare for a software dev interview.
> > Unfortunately, I misunderstood the problem. I did solve it
> > correctly (according to hackerrank) but I'm concerned about
> > inefficiency. Here is the link:
> > https://www.hackerrank.com/challenges/acm-icpc-team
> > However, since it's https that may mean that passwords etc.
> > are necessary. Here then is a copy-paste of the question (note
> > that the question is different to what I said here previously.)
> > Many thanks for your feedback.
> > Paul
> >
> > ********* BEGIN PROBLEM STATEMENT
> > Problem Statement
> >
> > You are given a list of N people who are attending ACM-ICPC
> > World Finals. Each of them are either well versed in a topic
> > or they are not. Find out the maximum number of topics
> > a 2-person team can know. And also find out how many teams
> > can know that maximum number of topics.
> >
> > Note Suppose a, b, and c are three different people,
> > then (a,b) and (b,c) are counted as two different teams.
> >
> > Input Format
> > The first line contains two integers, N and M, separated
> > by a single space, where N represents the number of people,
> > and M represents the number of topics. N lines follow.
> > Each line contains a binary string of length M. If the ith line's
> > jth character is 1, then the ith person knows the jth topic;
> > otherwise, he doesn't know the topic.
> >
> > Constraints
> > 2?N?500
> > 1?M?500
> >
> > Output Format
> > On the first line, print the maximum number of topics a
> > 2-person team can know. On the second line, print the number
> > of 2-person teams that can know the maximum number of topics.
> >
> > Sample Input
> > 4 5
> > 10101
> > 11100
> > 11010
> > 00101
> >
> > Sample Output
> > 5
> > 2
> >
> > Explanation
> > (1, 3) and (3, 4) know all the 5 topics. So the maximal topics
> > a 2-person team knows is 5, and only 2 teams can achieve this.
> > ***** END PROBLEM STATEMENT.
> >
> > Here is my solution with the #includes omitted.
> > int main() {
> > int n;
> > int m;
> > cin >> n >> m;
> > vector<string> topic(n);
> > for(int topic_i = 0;topic_i < n;topic_i++) {
> > cin >> topic[topic_i]; }
> > int numTopics = 0;
> > int knowers = 0;
> >
> > for(int i = 0; i < n; ++i)
> > for(int j = i + 1; j < n; ++j) {
> > int knownByPair = 0;
> > for(int k = 0; k < m; ++k) {
> > if(topic[i][k] == '1' || topic[j][k] == '1')
> > ++knownByPair; }
> > if(knownByPair > numTopics ) {
> > knowers = 1;
> > numTopics = knownByPair; }
> > else if(knownByPair == numTopics)
> > ++knowers;
> > }
> > cout << numTopics << endl << knowers;
> > return 0;
> > }
> >
> > I've spotted an added efficiency myself.
> > for(int k = 0; k < m; ++k) {
> > if(topic[i][k] == '1' || topic[j][k] == '1')
> > ++knownByPair; }
> > We don't actually need to go all the way to m if we already
> > know that knownByPair will be less than numTopics.
> > Hence
> > for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k) {
> > // body unchanged. }
> > This also passes the hackerrank test but doesn't feel
> > like a very substantial improvement.
> > Paul
> --
> John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2016-01-02 02:11 +0000 |
| Message-ID | <n67bjq$mde$1@reader1.panix.com> |
| In reply to | #325329 |
Paul <pepstein5@gmail.com> wrote:
> Thanks, John (and thanks also for your suggestions below).
> Since we've started top-posting, we might as well continue with
> this convention. Bitwise-or is the right concept. However,
> the right tool for this is bitset. I tried to apply your advice
> but to improve it by using bitset instead of several unsigned longs.
> The for loop now reads:
>
> for(int i = 0; i < n; ++i)
> for(int j = i + 1; j < n; ++j) {
> int knownByPair = 0;
> bitset<500> topicRep = bitset<500>(topic[i])
> | bitset<500>(topic[j]);
> for(int k = 0; k < m; ++k)
> knownByPair += topicRep[k];
> if(knownByPair > numTopics ) {
> knowers = 1;
> numTopics = knownByPair; }
> else if(knownByPair == numTopics)
> ++knowers;
> }
>
> This has been checked on hackerrank's platform and satisfies their tests.
> Is this what you have in mind, or are there still problems?
No problems as far as I can tell, offhand (just by looking).
You could arguably (though I might lose the argument) improve
the k=0...m-1 loop by "counting by bytes" like I suggested in
original follow-up. But I'm not sure how much improvement, if
any, that would actually accomplish. You already accomplished
the big improvement with the one-step 500-bit or, eliminating
that loop entirely. Maybe changing <500> to the actual input
<M> might be helpful. I'd imagine that if M<=8 or 32 or 64,
then cc -O3 might find trickier ways to generate the requisite
code for those kinds of cases.
> Thanks for your suggestion. I already have this book. However,
> I tend to read books from cover to cover (with the possible exception
> of some exercises) and I find the problem of finding chapters/pages
> to omit very difficult. So I'm not sure such a huge book is a way
> to optimise my progress. Accordingly, I'm reading Algorithms
> Unlocked instead.
Gee, reading an algorithms book cover-to-cover sounds like reading
a cookbook cover-to-cover. I usually focus on the dinner (problem)
at hand, and study the relevant recipes (algorithms). Of course,
the best way for you is whatever way you find best.
> Most famous books are available free online. However, getting
> non-pirated versions from virus-free websites is harder. I tried
> to research the website bayanbox.ir and couldn't find any info.
> Do you recommend this website more generally?
> Thanks, Paul
Nope, never heard of that site before google coughed it up while
I was looking for the publisher's url for the book, and found that
pdf instead. I downloaded the pdf myself, which looked okay to me
in several viewers. Didn't realize executable code could be embedded
in pdf's.
(P.S. the "probability generalization" in that other follow-up post
is actually kind of silly, since there's a totally trivial closed-form
solution that I overlooked: if p's the probability a bit is set,
then 1-(1-p)^2 is the "or" probability.)
> On Friday, January 1, 2016 at 2:19:21 AM UTC, John Forkosh wrote:
>> Excuse the top-posting, but just a few comments...
>> (a) right about your remark that you originally mis-stated the problem.
>> The problem statement clarifies what you wanted.
>> (b) here's a possibly better approach than the "brute force" approach
>> you have below, though the 500-topic max makes it a bit messier than
>> it might be: what you want is an (inclusive) "or" of all pairs of
>> bit strings. Each pair's a putative team. Once you've "or"'ed a team's
>> two bit strings, then just count the number of 1's in the "or", like
>> we all originally discussed. And the rest is just simple house-keeping.
>> If you can "or" a 64-bit unsigned long long, then that lets you
>> simultaneously (with one operation) check 64 topics. So just
>> declare unsigned long long people[500][8];, where 8*64=512
>> accommodates the 500 max topics. Then parse and interpret those
>> input strings accordingly during initialization. That'll be a
>> little harder than what you have below, but then the real work
>> should be a lot quicker.
>> (c) if you want to self-study algorithms, I think I can safely say
>> one of the best books is Cormen, et al's (long line follows),
>> http://bayanbox.ir/view/4177858657730907268/introduction-to-algorithms-3rd-edition.pdf
>> I'm surprised it's downloadable for free. You can just wget the whole thing.
>>
>> Paul <pepstein5@gmail.com> wrote:
>> > On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote:
>> >> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
>> >> > Paul <pepstein5@gmail.com> wrote:
>> >> > > Suppose we are given a collection of binary strings all of
>> >> > > the same length. The problem is to find the largest number n
>> >> > > such that there exist distinct strings s1, s2 such that n positions
>> >> > > are set to 1 in both s1 and s2 simultaneously. Also, the task
>> >> > > is to find all pairs so that this maximum is realised.
>> >> > >
>> >> > > This is easy to do but difficult to do efficiently. Could someone
>> >> > > help me, perhaps by referencing the right techniques to look at?
>> >> > >
>> >> > > No, this isn't a homework problem. No, I'm not enrolled in any
>> >> > > courses. I'm trying to learn some computer science.
>> >> > > Thanks, Paul
>> >> >
>> >> > (a) just curious: what suggested this problem to you?
>> >> > To me, as far as I can tell, it seems pretty useless,
>> >> > application-wise. And if you're "trying to learn some
>> >> > computer science", one of the first things to learn is
>> >> > recognizing useful or theoretically important problems.
>> >> > And, unless I'm wrong, this ain't one. But please correct
>> >> > me if I'm wrong about that -- then I'll learn something:).
>> >> >
>> >> > (b) there's efficient wrt computational complexity, and
>> >> > efficient wrt making efficient use of hardware architecture.
>> >> > I can't say about the first, but you can make use of 8-bit
>> >> > byte addressability of virtually all architectures, as follows.
>> >> > First, if n isn't a multiple of 8, concat some 0 bits to each
>> >> > string so that it is. Now munge through each string byte-by-byte.
>> >> > And have a 256-int table with nOnes[i]=the number of 1's for
>> >> > value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
>> >> > Then just sum up nOnes for each byte of a string. That'll reduce
>> >> > your loop by a factor of 8. (And I leave you to answer why munging
>> >> > through your string 32-bit-int by 32-bit-int isn't optimal.)
>> >> > And once you know the number of 1's in each string, the
>> >> > rest is easy ... except for uniqueness. If your collection
>> >> > of strings may contain duplicates, you'll also want to
>> >> > accumulate some md5-like hash of each string as you munge
>> >> > through them.
>> >> >
>> >> > (c) you just talking about learning, or you going to
>> >> > show us some code? Here's the function prototype in C,
>> >> > int nones ( unsigned char *s, int len )
>> >> > /* s = ptr to bit string (beginning on byte boundary) */
>> >> > /* len = total #bits in s */
>> >> > /* returns #bits in s that are 1's */
>> >> > Please follow up with that function...
>> >>
>> > Thanks to everyone for their comments. I'm doing challenge
>> > problems on hackerrank to prepare for a software dev interview.
>> > Unfortunately, I misunderstood the problem. I did solve it
>> > correctly (according to hackerrank) but I'm concerned about
>> > inefficiency. Here is the link:
>> > https://www.hackerrank.com/challenges/acm-icpc-team
>> > However, since it's https that may mean that passwords etc.
>> > are necessary. Here then is a copy-paste of the question (note
>> > that the question is different to what I said here previously.)
>> > Many thanks for your feedback.
>> > Paul
>> >
>> > ********* BEGIN PROBLEM STATEMENT
>> > Problem Statement
>> >
>> > You are given a list of N people who are attending ACM-ICPC
>> > World Finals. Each of them are either well versed in a topic
>> > or they are not. Find out the maximum number of topics
>> > a 2-person team can know. And also find out how many teams
>> > can know that maximum number of topics.
>> >
>> > Note Suppose a, b, and c are three different people,
>> > then (a,b) and (b,c) are counted as two different teams.
>> >
>> > Input Format
>> > The first line contains two integers, N and M, separated
>> > by a single space, where N represents the number of people,
>> > and M represents the number of topics. N lines follow.
>> > Each line contains a binary string of length M. If the ith line's
>> > jth character is 1, then the ith person knows the jth topic;
>> > otherwise, he doesn't know the topic.
>> >
>> > Constraints
>> > 2<=N<=500
>> > 1<=M<=500
>> >
>> > Output Format
>> > On the first line, print the maximum number of topics a
>> > 2-person team can know. On the second line, print the number
>> > of 2-person teams that can know the maximum number of topics.
>> >
>> > Sample Input
>> > 4 5
>> > 10101
>> > 11100
>> > 11010
>> > 00101
>> >
>> > Sample Output
>> > 5
>> > 2
>> >
>> > Explanation
>> > (1, 3) and (3, 4) know all the 5 topics. So the maximal topics
>> > a 2-person team knows is 5, and only 2 teams can achieve this.
>> > ***** END PROBLEM STATEMENT.
>> >
>> > Here is my solution with the #includes omitted.
>> > int main() {
>> > int n;
>> > int m;
>> > cin >> n >> m;
>> > vector<string> topic(n);
>> > for(int topic_i = 0;topic_i < n;topic_i++) {
>> > cin >> topic[topic_i]; }
>> > int numTopics = 0;
>> > int knowers = 0;
>> >
>> > for(int i = 0; i < n; ++i)
>> > for(int j = i + 1; j < n; ++j) {
>> > int knownByPair = 0;
>> > for(int k = 0; k < m; ++k) {
>> > if(topic[i][k] == '1' || topic[j][k] == '1')
>> > ++knownByPair; }
>> > if(knownByPair > numTopics ) {
>> > knowers = 1;
>> > numTopics = knownByPair; }
>> > else if(knownByPair == numTopics)
>> > ++knowers;
>> > }
>> > cout << numTopics << endl << knowers;
>> > return 0;
>> > }
>> >
>> > I've spotted an added efficiency myself.
>> > for(int k = 0; k < m; ++k) {
>> > if(topic[i][k] == '1' || topic[j][k] == '1')
>> > ++knownByPair; }
>> > We don't actually need to go all the way to m if we already
>> > know that knownByPair will be less than numTopics.
>> > Hence
>> > for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k) {
>> > // body unchanged. }
>> > This also passes the hackerrank test but doesn't feel
>> > like a very substantial improvement.
>> > Paul
>> --
>> John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | Paul <pepstein5@gmail.com> |
|---|---|
| Date | 2016-01-02 02:20 -0800 |
| Message-ID | <2f7d57ff-f16e-4aeb-bf71-987b2b25c15f@googlegroups.com> |
| In reply to | #325364 |
On Saturday, January 2, 2016 at 2:11:13 AM UTC, JohnF wrote:
> Paul <pepstein5@gmail.com> wrote:
> > Thanks, John (and thanks also for your suggestions below).
> > Since we've started top-posting, we might as well continue with
> > this convention. Bitwise-or is the right concept. However,
> > the right tool for this is bitset. I tried to apply your advice
> > but to improve it by using bitset instead of several unsigned longs.
> > The for loop now reads:
> >
> > for(int i = 0; i < n; ++i)
> > for(int j = i + 1; j < n; ++j) {
> > int knownByPair = 0;
> > bitset<500> topicRep = bitset<500>(topic[i])
> > | bitset<500>(topic[j]);
> > for(int k = 0; k < m; ++k)
> > knownByPair += topicRep[k];
> > if(knownByPair > numTopics ) {
> > knowers = 1;
> > numTopics = knownByPair; }
> > else if(knownByPair == numTopics)
> > ++knowers;
> > }
> >
> > This has been checked on hackerrank's platform and satisfies their tests.
> > Is this what you have in mind, or are there still problems?
>
> No problems as far as I can tell, offhand (just by looking).
> You could arguably (though I might lose the argument) improve
> the k=0...m-1 loop by "counting by bytes" like I suggested in
> original follow-up. But I'm not sure how much improvement, if
> any, that would actually accomplish. You already accomplished
> the big improvement with the one-step 500-bit or, eliminating
> that loop entirely. Maybe changing <500> to the actual input
> <M> might be helpful. I'd imagine that if M<=8 or 32 or 64,
> then cc -O3 might find trickier ways to generate the requisite
> code for those kinds of cases.
>
> > Thanks for your suggestion. I already have this book. However,
> > I tend to read books from cover to cover (with the possible exception
> > of some exercises) and I find the problem of finding chapters/pages
> > to omit very difficult. So I'm not sure such a huge book is a way
> > to optimise my progress. Accordingly, I'm reading Algorithms
> > Unlocked instead.
>
> Gee, reading an algorithms book cover-to-cover sounds like reading
> a cookbook cover-to-cover. I usually focus on the dinner (problem)
> at hand, and study the relevant recipes (algorithms). Of course,
> the best way for you is whatever way you find best.
>
> > Most famous books are available free online. However, getting
> > non-pirated versions from virus-free websites is harder. I tried
> > to research the website bayanbox.ir and couldn't find any info.
> > Do you recommend this website more generally?
> > Thanks, Paul
>
> Nope, never heard of that site before google coughed it up while
> I was looking for the publisher's url for the book, and found that
> pdf instead. I downloaded the pdf myself, which looked okay to me
> in several viewers. Didn't realize executable code could be embedded
> in pdf's.
>
> (P.S. the "probability generalization" in that other follow-up post
> is actually kind of silly, since there's a totally trivial closed-form
> solution that I overlooked: if p's the probability a bit is set,
> then 1-(1-p)^2 is the "or" probability.)
>
> > On Friday, January 1, 2016 at 2:19:21 AM UTC, John Forkosh wrote:
> >> Excuse the top-posting, but just a few comments...
> >> (a) right about your remark that you originally mis-stated the problem.
> >> The problem statement clarifies what you wanted.
> >> (b) here's a possibly better approach than the "brute force" approach
> >> you have below, though the 500-topic max makes it a bit messier than
> >> it might be: what you want is an (inclusive) "or" of all pairs of
> >> bit strings. Each pair's a putative team. Once you've "or"'ed a team's
> >> two bit strings, then just count the number of 1's in the "or", like
> >> we all originally discussed. And the rest is just simple house-keeping.
> >> If you can "or" a 64-bit unsigned long long, then that lets you
> >> simultaneously (with one operation) check 64 topics. So just
> >> declare unsigned long long people[500][8];, where 8*64=512
> >> accommodates the 500 max topics. Then parse and interpret those
> >> input strings accordingly during initialization. That'll be a
> >> little harder than what you have below, but then the real work
> >> should be a lot quicker.
> >> (c) if you want to self-study algorithms, I think I can safely say
> >> one of the best books is Cormen, et al's (long line follows),
> >> http://bayanbox.ir/view/4177858657730907268/introduction-to-algorithms-3rd-edition.pdf
> >> I'm surprised it's downloadable for free. You can just wget the whole thing.
> >>
> >> Paul <pepstein5@gmail.com> wrote:
> >> > On Thursday, December 31, 2015 at 2:46:55 PM UTC, Paul wrote:
> >> >> On Thursday, December 31, 2015 at 6:53:34 AM UTC, JohnF wrote:
> >> >> > Paul <pepstein5@gmail.com> wrote:
> >> >> > > Suppose we are given a collection of binary strings all of
> >> >> > > the same length. The problem is to find the largest number n
> >> >> > > such that there exist distinct strings s1, s2 such that n positions
> >> >> > > are set to 1 in both s1 and s2 simultaneously. Also, the task
> >> >> > > is to find all pairs so that this maximum is realised.
> >> >> > >
> >> >> > > This is easy to do but difficult to do efficiently. Could someone
> >> >> > > help me, perhaps by referencing the right techniques to look at?
> >> >> > >
> >> >> > > No, this isn't a homework problem. No, I'm not enrolled in any
> >> >> > > courses. I'm trying to learn some computer science.
> >> >> > > Thanks, Paul
> >> >> >
> >> >> > (a) just curious: what suggested this problem to you?
> >> >> > To me, as far as I can tell, it seems pretty useless,
> >> >> > application-wise. And if you're "trying to learn some
> >> >> > computer science", one of the first things to learn is
> >> >> > recognizing useful or theoretically important problems.
> >> >> > And, unless I'm wrong, this ain't one. But please correct
> >> >> > me if I'm wrong about that -- then I'll learn something:).
> >> >> >
> >> >> > (b) there's efficient wrt computational complexity, and
> >> >> > efficient wrt making efficient use of hardware architecture.
> >> >> > I can't say about the first, but you can make use of 8-bit
> >> >> > byte addressability of virtually all architectures, as follows.
> >> >> > First, if n isn't a multiple of 8, concat some 0 bits to each
> >> >> > string so that it is. Now munge through each string byte-by-byte.
> >> >> > And have a 256-int table with nOnes[i]=the number of 1's for
> >> >> > value i, e.g., nOnes[0]=0, [3]=2, [16]=1, [255]=8.
> >> >> > Then just sum up nOnes for each byte of a string. That'll reduce
> >> >> > your loop by a factor of 8. (And I leave you to answer why munging
> >> >> > through your string 32-bit-int by 32-bit-int isn't optimal.)
> >> >> > And once you know the number of 1's in each string, the
> >> >> > rest is easy ... except for uniqueness. If your collection
> >> >> > of strings may contain duplicates, you'll also want to
> >> >> > accumulate some md5-like hash of each string as you munge
> >> >> > through them.
> >> >> >
> >> >> > (c) you just talking about learning, or you going to
> >> >> > show us some code? Here's the function prototype in C,
> >> >> > int nones ( unsigned char *s, int len )
> >> >> > /* s = ptr to bit string (beginning on byte boundary) */
> >> >> > /* len = total #bits in s */
> >> >> > /* returns #bits in s that are 1's */
> >> >> > Please follow up with that function...
> >> >>
> >> > Thanks to everyone for their comments. I'm doing challenge
> >> > problems on hackerrank to prepare for a software dev interview.
> >> > Unfortunately, I misunderstood the problem. I did solve it
> >> > correctly (according to hackerrank) but I'm concerned about
> >> > inefficiency. Here is the link:
> >> > https://www.hackerrank.com/challenges/acm-icpc-team
> >> > However, since it's https that may mean that passwords etc.
> >> > are necessary. Here then is a copy-paste of the question (note
> >> > that the question is different to what I said here previously.)
> >> > Many thanks for your feedback.
> >> > Paul
> >> >
> >> > ********* BEGIN PROBLEM STATEMENT
> >> > Problem Statement
> >> >
> >> > You are given a list of N people who are attending ACM-ICPC
> >> > World Finals. Each of them are either well versed in a topic
> >> > or they are not. Find out the maximum number of topics
> >> > a 2-person team can know. And also find out how many teams
> >> > can know that maximum number of topics.
> >> >
> >> > Note Suppose a, b, and c are three different people,
> >> > then (a,b) and (b,c) are counted as two different teams.
> >> >
> >> > Input Format
> >> > The first line contains two integers, N and M, separated
> >> > by a single space, where N represents the number of people,
> >> > and M represents the number of topics. N lines follow.
> >> > Each line contains a binary string of length M. If the ith line's
> >> > jth character is 1, then the ith person knows the jth topic;
> >> > otherwise, he doesn't know the topic.
> >> >
> >> > Constraints
> >> > 2<=N<=500
> >> > 1<=M<=500
> >> >
> >> > Output Format
> >> > On the first line, print the maximum number of topics a
> >> > 2-person team can know. On the second line, print the number
> >> > of 2-person teams that can know the maximum number of topics.
> >> >
> >> > Sample Input
> >> > 4 5
> >> > 10101
> >> > 11100
> >> > 11010
> >> > 00101
> >> >
> >> > Sample Output
> >> > 5
> >> > 2
> >> >
> >> > Explanation
> >> > (1, 3) and (3, 4) know all the 5 topics. So the maximal topics
> >> > a 2-person team knows is 5, and only 2 teams can achieve this.
> >> > ***** END PROBLEM STATEMENT.
> >> >
> >> > Here is my solution with the #includes omitted.
> >> > int main() {
> >> > int n;
> >> > int m;
> >> > cin >> n >> m;
> >> > vector<string> topic(n);
> >> > for(int topic_i = 0;topic_i < n;topic_i++) {
> >> > cin >> topic[topic_i]; }
> >> > int numTopics = 0;
> >> > int knowers = 0;
> >> >
> >> > for(int i = 0; i < n; ++i)
> >> > for(int j = i + 1; j < n; ++j) {
> >> > int knownByPair = 0;
> >> > for(int k = 0; k < m; ++k) {
> >> > if(topic[i][k] == '1' || topic[j][k] == '1')
> >> > ++knownByPair; }
> >> > if(knownByPair > numTopics ) {
> >> > knowers = 1;
> >> > numTopics = knownByPair; }
> >> > else if(knownByPair == numTopics)
> >> > ++knowers;
> >> > }
> >> > cout << numTopics << endl << knowers;
> >> > return 0;
> >> > }
> >> >
> >> > I've spotted an added efficiency myself.
> >> > for(int k = 0; k < m; ++k) {
> >> > if(topic[i][k] == '1' || topic[j][k] == '1')
> >> > ++knownByPair; }
> >> > We don't actually need to go all the way to m if we already
> >> > know that knownByPair will be less than numTopics.
> >> > Hence
> >> > for(int k = 0; knownByPair + m - k >= numTopics && k < m; ++k) {
> >> > // body unchanged. }
> >> > This also passes the hackerrank test but doesn't feel
> >> > like a very substantial improvement.
> >> > Paul
> >> --
> >> John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
> --
> John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
John,
Thanks for your comments. bitset<M>; won't compile unless M is a constant. That's why I used bitset<500> Do you know of a workaround (other than coding in another language)?
Attempting to solve problems and using a book as a reference only when stuck has many difficulties. One drawback is that deficits in a person's knowledge can go unrepaired. For example, a problem might involve sorting and a person might only know O(n^2) methods, and might happily code such a solution without consulting the algorithms book, and never learn the more efficient methods.
My idea is to read Algorithms Unlocked (simply because it's like CLRS and much shorter) and then attempt problems from hackerrank.
I've also never heard of executable code being embedded in pdf's. The point I was trying to make is that there are lots of pirate sites giving away free ebooks online without the publishers' consent. (I have no idea whether your recommended download comes from one of those sites). Downloading (and even visiting without downloading) such sites is fraught with virus risk. As far as I know, the risk is not generally associated with the pdf document itself. Here I'm using "virus" as a generic term for "bad alterations to your computer". The most usual problem (in cases where there is a problem) is that activating the download secretly downloads other stuff besides the pdf and it is these other files that cause the problem. Malware is the correct term, I think.
Paul
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2016-01-02 12:50 +0000 |
| Message-ID | <n68h26$h24$1@reader1.panix.com> |
| In reply to | #325378 |
Paul <pepstein5@gmail.com> wrote:
<<snip>>
> Thanks for your comments. bitset<M>; won't compile
> unless M is a constant.
I wasn't aware of that. In fact, I usually code in C,
and wasn't aware of bitset at all until you mentioned
it and I looked it up.
> That's why I used bitset<500>
That would explain it.
> Do you know of a workaround (other than coding in another language)?
Nope... how about
switch(M) {
case 1: bitset<1> etc; break;
case 2: bitset<2> etc; break;
default: bitset<500> etc; break; }
just kidding. Though you could have just a few cases
if ( M <= 100 ) { bitset<100> etc; }
else if ( M <= 200 } { you_know_the_rest; }
That always-<500> default's gotta cost a lot of
usually-unnecessary work. And efficiency's apparently
a sine qua non for this exercise.
> Attempting to solve problems and using a book as a reference only
> when stuck has many difficulties. One drawback is that deficits
> in a person's knowledge can go unrepaired. For example, a problem
> might involve sorting and a person might only know O(n^2) methods,
> and might happily code such a solution without consulting the
> algorithms book, and never learn the more efficient methods.
What comes to mind for me is that it took me a while to come across
recursive descent parsing, which would have neatened up a lot of
my code if I'd known the technique sooner. And I'm sure there are
other similar cases that I still don't know about.
> My idea is to read Algorithms Unlocked (simply because it's like
> CLRS and much shorter) and then attempt problems from hackerrank.
I've got a print copy of CLRS first edition, from way back.
Maybe Algorithms Unlocked might be worth a look. Got a url for that?:)
> I've also never heard of executable code being embedded in pdf's.
> The point I was trying to make is that there are lots of pirate
> sites giving away free ebooks online without the publishers' consent.
> (I have no idea whether your recommended download comes from one of
> those sites). Downloading (and even visiting without downloading)
> such sites is fraught with virus risk. As far as I know, the risk
> is not generally associated with the pdf document itself. Here
> I'm using "virus" as a generic term for "bad alterations to your
> computer". The most usual problem (in cases where there is a problem)
> is that activating the download secretly downloads other stuff besides
> the pdf and it is these other files that cause the problem. Malware
> is the correct term, I think.
> Paul
Yeah, I used wget to download the pdf, without even visiting the site.
And I guess I could have done that from my shell account on my ISP,
and then scp'ed it to my pc from there. And, yeah, I'd guess the pdf
isn't legal.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | Paul <pepstein5@gmail.com> |
|---|---|
| Date | 2016-01-02 07:12 -0800 |
| Message-ID | <e65d054d-35b6-4ab6-80e3-2b60a9d6128b@googlegroups.com> |
| In reply to | #325379 |
On Saturday, January 2, 2016 at 12:50:27 PM UTC, JohnF wrote:
> Paul <pepstein5@gmail.com> wrote:
> <<snip>>
> > Thanks for your comments. bitset<M>; won't compile
> > unless M is a constant.
> I wasn't aware of that. In fact, I usually code in C,
> and wasn't aware of bitset at all until you mentioned
> it and I looked it up.
>
> > That's why I used bitset<500>
> That would explain it.
>
> > Do you know of a workaround (other than coding in another language)?
> Nope... how about
> switch(M) {
> case 1: bitset<1> etc; break;
> case 2: bitset<2> etc; break;
> default: bitset<500> etc; break; }
> just kidding. Though you could have just a few cases
> if ( M <= 100 ) { bitset<100> etc; }
> else if ( M <= 200 } { you_know_the_rest; }
> That always-<500> default's gotta cost a lot of
> usually-unnecessary work. And efficiency's apparently
> a sine qua non for this exercise.
>
> > Attempting to solve problems and using a book as a reference only
> > when stuck has many difficulties. One drawback is that deficits
> > in a person's knowledge can go unrepaired. For example, a problem
> > might involve sorting and a person might only know O(n^2) methods,
> > and might happily code such a solution without consulting the
> > algorithms book, and never learn the more efficient methods.
>
> What comes to mind for me is that it took me a while to come across
> recursive descent parsing, which would have neatened up a lot of
> my code if I'd known the technique sooner. And I'm sure there are
> other similar cases that I still don't know about.
>
> > My idea is to read Algorithms Unlocked (simply because it's like
> > CLRS and much shorter) and then attempt problems from hackerrank.
>
> I've got a print copy of CLRS first edition, from way back.
> Maybe Algorithms Unlocked might be worth a look. Got a url for that?:)
>
> > I've also never heard of executable code being embedded in pdf's.
> > The point I was trying to make is that there are lots of pirate
> > sites giving away free ebooks online without the publishers' consent.
> > (I have no idea whether your recommended download comes from one of
> > those sites). Downloading (and even visiting without downloading)
> > such sites is fraught with virus risk. As far as I know, the risk
> > is not generally associated with the pdf document itself. Here
> > I'm using "virus" as a generic term for "bad alterations to your
> > computer". The most usual problem (in cases where there is a problem)
> > is that activating the download secretly downloads other stuff besides
> > the pdf and it is these other files that cause the problem. Malware
> > is the correct term, I think.
> > Paul
>
> Yeah, I used wget to download the pdf, without even visiting the site.
> And I guess I could have done that from my shell account on my ISP,
> and then scp'ed it to my pc from there. And, yeah, I'd guess the pdf
> isn't legal.
John,
I am ignorant about Gnu and related issues. I had no idea what wget meant and read it as a typo for "get". Good that you are using safe methods to get downloads. Algorithms Unlocked is available via vk.com I will link to the download page and also another page about vk.com to enable you to decide whether you want to support their site. vk.com is described here: https://en.wikipedia.org/wiki/VK_(social_networking)
They allow you to download the book here: https://vk.com/doc10903696_190637386?hash=a53c5f15549ac496d2&dl=8c0954f5b4aea05a0a
I had not heard of recursive descent parsing. I am interviewing for software dev jobs and want to limit my searching to areas of vocational value. Outside academia, do you think that this technique is valuable to employers? If so, I will look into it.
Thanks again for your help.
Paul
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2016-01-03 04:55 +0000 |
| Message-ID | <n6a9ja$le7$1@reader1.panix.com> |
| In reply to | #325386 |
Paul <pepstein5@gmail.com> wrote:
> On Saturday, January 2, 2016 at 12:50:27 PM UTC, JohnF wrote:
>> Paul <pepstein5@gmail.com> wrote:
>> <<snip>>
>> > Thanks for your comments. bitset<M>; won't compile
>> > unless M is a constant.
>> I wasn't aware of that. In fact, I usually code in C,
>> and wasn't aware of bitset at all until you mentioned
>> it and I looked it up.
>>
>> > That's why I used bitset<500>
>> That would explain it.
>>
>> > Do you know of a workaround (other than coding in another language)?
>> Nope... how about
>> switch(M) {
>> case 1: bitset<1> etc; break;
>> case 2: bitset<2> etc; break;
>> default: bitset<500> etc; break; }
>> just kidding. Though you could have just a few cases
>> if ( M <= 100 ) { bitset<100> etc; }
>> else if ( M <= 200 } { you_know_the_rest; }
>> That always-<500> default's gotta cost a lot of
>> usually-unnecessary work. And efficiency's apparently
>> a sine qua non for this exercise.
>>
>> > Attempting to solve problems and using a book as a reference only
>> > when stuck has many difficulties. One drawback is that deficits
>> > in a person's knowledge can go unrepaired. For example, a problem
>> > might involve sorting and a person might only know O(n^2) methods,
>> > and might happily code such a solution without consulting the
>> > algorithms book, and never learn the more efficient methods.
>>
>> What comes to mind for me is that it took me a while to come across
>> recursive descent parsing, which would have neatened up a lot of
>> my code if I'd known the technique sooner. And I'm sure there are
>> other similar cases that I still don't know about.
>>
>> > My idea is to read Algorithms Unlocked (simply because it's like
>> > CLRS and much shorter) and then attempt problems from hackerrank.
>>
>> I've got a print copy of CLRS first edition, from way back.
>> Maybe Algorithms Unlocked might be worth a look. Got a url for that?:)
>>
>> > I've also never heard of executable code being embedded in pdf's.
>> > The point I was trying to make is that there are lots of pirate
>> > sites giving away free ebooks online without the publishers' consent.
>> > (I have no idea whether your recommended download comes from one of
>> > those sites). Downloading (and even visiting without downloading)
>> > such sites is fraught with virus risk. As far as I know, the risk
>> > is not generally associated with the pdf document itself. Here
>> > I'm using "virus" as a generic term for "bad alterations to your
>> > computer". The most usual problem (in cases where there is a problem)
>> > is that activating the download secretly downloads other stuff besides
>> > the pdf and it is these other files that cause the problem. Malware
>> > is the correct term, I think.
>> > Paul
>>
>> Yeah, I used wget to download the pdf, without even visiting the site.
>> And I guess I could have done that from my shell account on my ISP,
>> and then scp'ed it to my pc from there. And, yeah, I'd guess the pdf
>> isn't legal.
>
> John,
> I am ignorant about Gnu and related issues.
> I had no idea what wget meant and read it as a typo for "get".
> Good that you are using safe methods to get downloads.
My bad, sorry about that, I just assumed linux (and friends)
was de rigueur among programmers. And I'm now assuming you've
googled wget and know all you need to. (see below about "safe")
> Algorithms Unlocked is available via vk.com I will link to the
> download page and also another page about vk.com to enable you
> to decide whether you want to support their site.
> vk.com is described here:
> https://en.wikipedia.org/wiki/VK_(social_networking)
Well, part of that page says,
"VK grants easy access to pornographic images, video and
other such materials..."
so "support" isn't exactly the word describing what I'd like
to do to them.
> They allow you to download the book here:
> https://vk.com/doc10903696_190637386?hash=a53c5f15549ac496d2&dl=8c0954f5b4aea05a0a
Thanks for the link, Paul. The "safe" trick here is to first wget
that doc109... page. Then viewing its source reveals
function saveDoc() {
var src = 'https://psv4.vk.me/c537109/u10903696/docs/652b58883d71/Algorithms_Unlocked.pdf?extra=wcHQkQX9yBQ5X...etc'
And so the direct link to the pdf is just
https://psv4.vk.me/c537109/u10903696/docs/652b58883d71/Algorithms_Unlocked.pdf
and now you can wget that pdf directly, without ever browser-visiting
any page at all. And note the copyright's MIT Press 2013, so I suppose
we can conclude it's pirated. But my cursory examination is that
you're right -- it's a great book. Indeed, the preface and intro are
pretty much word-for-word taken from its bigger brother.
> I had not heard of recursive descent parsing. I am interviewing
> for software dev jobs and want to limit my searching to areas
> of vocational value. Outside academia, do you think that this
> technique is valuable to employers? If so, I will look into it.
> Thanks again for your help.
> Paul
Well, I >>don't<< think it's likely to be valuable on an interview.
And neither Cormen book discusses it, but it's pretty much
chapter one in many compiler design books. If you have the bnf grammar
for a language you need to parse, then recursive descent's the
straightforward and elegant way to do it. I haven't written any
compilers, but lots of applications I've written required designing
a "little language" (very little) for user input. And my brute force
parsing required way more coding than should have been necessary.
Even parsing something as simple as a ?query_string of the form
<variable>=<value>&<variable>=<value>&etc is likely to benefit.
(You can google recursive descent bnf for more info, if interested.)
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [standalone]
Back to top | Article view | sci.math
csiph-web