If you have google its like a one click sign in and im not here to promote the site even tho i like it alot. Anyways.
I've always had a vague understanding of binary. This problem seems perfect for a binary type solution bc it should add some level of efficiency as opposed to doing the addition one digit at a time. Least i'd assume. So to the problem i'm having.
Taking a string and converting it to binary seems to work fine via
void strtobiarr(string& str){
unsigned long long tmp=0;
int yy=sizeof(unsigned long long)-1;
for(int x=0;x<str.size();x++){
if (str[x]=='1'){
tmp^= 1<<yy;
}
// cout<<bitset<sizeof(8)>(tmp)<<'\n';
yy--;
if(yy==-1){
yy=sizeof(unsigned long long)-1;
biarr.push_back(tmp);
tmp=0;
}
}
}
The issue i'm having with this is that i can't seem to get more than 8 bits to attach to tmp? I realize from what i posted that would make sense but if i change the range from 63-0 some really weird stuff starts happening. From what i've seen an ULL is supposed to be 64 bits? 8 bytes * 8 bits. However nothing i seem to do allows me to actually store 64 bits. I'm attempting to create a struct that breaks the string up into a vector of vector<unsigned long long> 's and then use operator overloads on the elements to do various operations which everything seems to work pretty smoothly with 8 bits but if all i'm accomplishing is taking one iteration through and dividing it by 8 its hardly worth the effort. Any help would be appreciated. Thanks guys.
I am not sure ULL has to be 64 bits. I think it 'usually is'. Its 64 on my compiler.
if you want to be sure, use the named ones. uint64_t type stuff that tells you how many bits you want. Endian matters.
thats a struct member variable. I wasnt going to post it all. I will if you want me to. I didn't think that variable had much relevance as to what the code is trying to accomplish which is compare the char, set the bit or not and print it out.
Looks like I can read the problem without an account. I can't submit it though; try this:
I wasn't seeking an easy answer but what you have posted is definitely inline with the path i was traveling. I had the problem solved with the test cases that they provide the thing that was getting me was the string being up to [500]. Was not aware that i could use bitset as a datatype... that might have been useful 3 hrs ago ha!.
@Jonin the reason why i was trying to view output as binary was so that i could see if it matched up with the input after i was done shuffling it around etc.
i'm going to see what i can do with that bitset data type, that will definitely simplify a few things. Thanks guys i'll report back here in a few.
ok sweet this actually gives me the output that i'm expecting even though for some reason i have to write to the bitset vector in reverse order but progress. Super sweet.
I was thinking i could make the bitset sizes more dynamic to shave off time but it passed thats all i care about.
after thinking about the direction the bitset was written i guess it didn't really even matter. the result of the or operation would have been the same regardless. I will definitely say this is a handy tool to have bc theres so many problems that get dumbed down to true or false and being able to weed out differences between sets of objects en mass via 1 operation. fantastic.
Dynamic sizing of bitsets is a problem hence the 500 to cover all cases.
It's easy enough to extract the data from the sample file as bitsets and convert the bits to strings of length m as below, even if you aren't in a position to use stringview.
Despite that maybe just stick to bitsets and use OR/AND blah blah on the two team members over the range of m and keep count of that. i.e. conversion to strings most likely isn't needed.
I was playing around with it for a bit. seems like its pretty well hell bent against it. It allows you to use sizeof() but only so much can be done with that. I was looking at some of the built in functions for bitset and it figures theres a function that returns a count of the bits that are set... good for future reference tho.
@jonin with this particular exercise tho if you used a vector bool then it would almost defeat the purpose of doin any conversion away from the string to begin with. i'm sure there would be some speed benefit due to the more efficient nature of the vector bool but wouldn't the conversion process negate it? Unless there is a way to or 2 vector bools together. As i said the added benefit using the bitset is that count method.
@againstry my final answer is what it is bc i'm only saving the relevant information. The problem only requires the answer have the number of sets of teams that have the maximum number of subjects and what the max subjects number is of the combined teams. Part of the reason to shoot for maximum efficiency bc if the code doesn't run under a certain time on these tests it will fail you. That is usually the difference between a couple extra memory operations like push_backs or an extra loop or 2. Your code looks legit, it would probably get the job done. The only thing i really don't get is why you're creating attendee on the heap? I realize this is just playing around but shouldn't you delete when you're done with it?
I don't know if you can swindle a vec bool into a 64 bit register and do it in chunks efficiently or not. I have not tried. Its one of those things that if you are unhappy with your best performance, you can start fooling with exotic alternate approaches...
(not even bitset can or something bigger than 1 register in one clock... ) it may be fast, but its still going to have to iterate if you exceed the hardware.
@martyrocks
I am reproducing the sample info and step by step making sure of that, hence all the display stuff etc. All of that would be cut out, along with the struct etc, if I wanted to submit it.
attendee on the heap is because they specify how many and therefore why not do the absolute minimum instead of assuming the maximum was the reason.
delete should be there but program ending solves that - if not they’re in for a shock ;)
Looks good. The only thing that I'd probably change is the attendee being created on the heap, the count array is probably unnecessary, i also like using vectors bc you can use .size() instead of a separate counter but bitset on its own has its own counter which is conveniently also .size(). Bitset is probably a template placed over top of a vector bool anyways so I guess that would make sense. I like how you or'ed them together then used .count() on the result. Tricky.
My keyboard on my laptop broke for no apparent reason. Idk wtf happened. Must have beat it to death. Ridiculous.
You should join hackerrank the site is amazing. Improving my skills every day.