• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 09:32
CET 15:32
KST 23:32
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13
Community News
[TLMC] Fall/Winter 2025 Ladder Map Rotation12Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA8StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7
StarCraft 2
General
Mech is the composition that needs teleportation t RotterdaM "Serral is the GOAT, and it's not close" RSL Season 3 - RO16 Groups C & D Preview [TLMC] Fall/Winter 2025 Ladder Map Rotation TL.net Map Contest #21: Winners
Tourneys
RSL Revival: Season 3 Sparkling Tuna Cup - Weekly Open Tournament Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
FlaSh on: Biggest Problem With SnOw's Playstyle BW General Discussion What happened to TvZ on Retro? Brood War web app to calculate unit interactions [ASL20] Ask the mapmakers — Drop your questions
Tourneys
[Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL21] RO32 Group D - Sunday 21:00 CET [BSL21] RO32 Group C - Saturday 21:00 CET
Strategy
PvZ map balance Current Meta Simple Questions, Simple Answers How to stay on top of macro?
Other Games
General Games
Path of Exile Stormgate/Frost Giant Megathread Nintendo Switch Thread Clair Obscur - Expedition 33 Beyond All Reason
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread Artificial Intelligence Thread Canadian Politics Mega-thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Dyadica Gospel – a Pulp No…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2375 users

An interesting complex programming problem

Blogs > Qzy
Post a Reply
1 2 Next All
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2011-05-21 15:55:19
May 21 2011 15:44 GMT
#1
Hi programmers/math people.

Okay, here's the problem.
I have a hashmap with Strings as keys and values pointing to objects (as seen below in java)

HashMap<String, SomeObject>

The chars within a single string is element of the set {0, 1, #}. # is a wildcard which can represent either a 0 or 1.

When presented by a message, ie: 011010111 (a message's char is element of the set {0, 1}), the following strings are satisfied:
01#01#111
#1101011#
011010111
#########
etc., due to their wildcards.

Which look up/sorting method would you do, such that you have the fastest algorithm to store the strings and also find the strings which are satisfied?

Bruteforce
Complexity: Finding all satisfied strings: O(n*p) with n = population of strings, p = size of string.

Bruteforce works ofcourse:
for(all strings in hashmap)
is string satisfied? Save it
next string

Tree
Complexity: O(p*n), but very unlikely that all strings are found in ONE leaf. Constructing the tree O(2^p) (!HOLY FUCK!)

Keeping a tree which branches every time a wildcard apears in a string. Each leaf in the tree has a hashset, which looks like the one above. The string's SomeObject ie, 01#01#111 would be possible to find in 4 leafs of the tree:
010010111
010011111
011011111
011010111

The problem is constructing the tree... if the String is big like 20-30 chars, the construction is simply too big to be possible.

How would you do it?

*****
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Cube
Profile Blog Joined February 2008
Canada777 Posts
Last Edited: 2011-05-21 16:21:33
May 21 2011 16:03 GMT
#2
what I want to do is solve the problem by "folding" the strings into unique integers somehow, but i'm not sure it can be done.

edit: I really don't think I can help you, sorry.

edit2: what about making a new hashmap with no wildcards by replicating each string/object pairing 2^(num #s) times, then sorting the strings as integers. (big setup time, subsequent searches are O(lgn)).
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2011-05-21 16:29:24
May 21 2011 16:23 GMT
#3
On May 22 2011 01:03 Cube wrote:
what I want to do is solve the problem by "folding" the strings into unique integers somehow, but i'm not sure it can be done.

edit: I really don't think I can help you, sorry.


Might actually be a good idea.

Then it's an experiment of how much fold it required to sort it into serveral small hashmaps
Ignore what i wrote, I gotta think more about it.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Famulus
Profile Joined April 2011
United States8 Posts
May 21 2011 16:33 GMT
#4
What about bruteforcing the other way. Assuming you only care about getting the correct object and not the actual string, make an entry in the hash table for every possible message for each string with a wildcard.
pullarius1
Profile Blog Joined May 2010
United States523 Posts
Last Edited: 2011-05-21 16:35:48
May 21 2011 16:34 GMT
#5
Just to clarify, there are no limits on the sizes or types of data, eg a string could be a million characters long and the population of acceptable strings could be arbitrarily large? Also, are we assuming that all strings we're working with are of the same length?

I guess what I'm really asking is whether the problem is for an actual project in real life or just a theoretical puzzle?
@pullarius1
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
May 21 2011 16:38 GMT
#6
On May 22 2011 01:33 Famulus wrote:
What about bruteforcing the other way. Assuming you only care about getting the correct object and not the actual string, make an entry in the hash table for every possible message for each string with a wildcard.


It would be a good idea, but every possible message is 2^(length of string), that's

length -> combinations
20 -> 1,048,576
40 -> 1,099,511,627,776 (in my case)

Not scalable :/.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
May 21 2011 16:40 GMT
#7
On May 22 2011 01:34 pullarius1 wrote:
Just to clarify, there are no limits on the sizes or types of data, eg a string could be a million characters long and the population of acceptable strings could be arbitrarily large? Also, are we assuming that all strings we're working with are of the same length?

I guess what I'm really asking is whether the problem is for an actual project in real life or just a theoretical puzzle?


Perfectly good questions. All strings are the same length, and so is the message that needs to be satisfied. It's for an XCS engine - I'll provide a paper in a sec.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2011-05-21 16:49:05
May 21 2011 16:43 GMT
#8
algorithmic description of XCS

It's an AI learning technique, based on "Learning classifier systems". You don't really need to read it to understand the problem though.

Bunch of strings with #10 in it, and a message with 10 which needs to find the strings that satisfies it.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
arioch
Profile Joined May 2010
England403 Posts
May 21 2011 17:15 GMT
#9
I am interested to see if someone comes up with an alternative to iteration for this as I parse huge data files on a daily basis for work.

I often find myself setting up foreach loops with regular expressions to loop through hashtables in perl, and always wondered if there was a more efficient way of doing it.
Mx.DeeP
Profile Joined February 2008
China25 Posts
May 21 2011 17:18 GMT
#10
If you're not worried about memory, you can just take the initial HashMap and convert it into a new HashMap<String, ArrayList<SomeObject>> where the key is only {0,1}. You just iterate through the original HashMap and convert all '#' into '0' and '1'. This is worst case O(2^p) for a String of all '#' for storing, but gives you O(1) look-up time.
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2011-05-21 17:33:34
May 21 2011 17:32 GMT
#11
On May 22 2011 02:18 Mx.DeeP wrote:
If you're not worried about memory, you can just take the initial HashMap and convert it into a new HashMap<String, ArrayList<SomeObject>> where the key is only {0,1}. You just iterate through the original HashMap and convert all '#' into '0' and '1'. This is worst case O(2^p) for a String of all '#' for storing, but gives you O(1) look-up time.


Exactly - that's the "tree" i talked about..

My message (in my problem) has 40 bits, that's 2^40 in construction of that tree... 1,099,511,627,776 nodes in it - would take too long :/.

I'm gonna go work out, and think about it. .
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
pullarius1
Profile Blog Joined May 2010
United States523 Posts
Last Edited: 2011-05-21 17:34:14
May 21 2011 17:32 GMT
#12
I'm not sure how much you get to work with the lists beforehand, but obviously if you could sort the list of wild strings before hand it would help a lot. But it would be a waste of time if the number of strings you were checking for matches for were very low. That is, if you have 100 01# strings, but only needed to find the matches for a few 10 strings, sorting would probably hurt. But if you had 100 strings to match, it would probably be worth your while.

One thing I thought of that is probably not useful at all:
For each string, rehash it into integers in the following way-
For each placenumber i, assign the the 2i-th and (2i-1)th prime to it. If that place number holds a 1 one, choose the odd prime, a 0, choose the even prime, a # choose neither. Multiply all the chosen primes together.

For instance 10110 would be (2 or 3) (5 or 7) (11 or 13) (17 or 19) (23 or 29) 2*7*11*17*29 = 75,922

While #01#0 would be (2 or 3) (5 or 7) (11 or 13) (17 or 19) (23 or29) 7*11*29 = 2,233

The benefit of this system would be that wild strings would divide precisely the strings that satisfied them. For whatever that's worth.

...sometimes I wish I had taken some practical programming classes in school :-(.

@pullarius1
Cube
Profile Blog Joined February 2008
Canada777 Posts
May 21 2011 18:07 GMT
#13
On May 22 2011 02:32 pullarius1 wrote:
I'm not sure how much you get to work with the lists beforehand, but obviously if you could sort the list of wild strings before hand it would help a lot. But it would be a waste of time if the number of strings you were checking for matches for were very low. That is, if you have 100 01# strings, but only needed to find the matches for a few 10 strings, sorting would probably hurt. But if you had 100 strings to match, it would probably be worth your while.

One thing I thought of that is probably not useful at all:
For each string, rehash it into integers in the following way-
For each placenumber i, assign the the 2i-th and (2i-1)th prime to it. If that place number holds a 1 one, choose the odd prime, a 0, choose the even prime, a # choose neither. Multiply all the chosen primes together.

For instance 10110 would be (2 or 3) (5 or 7) (11 or 13) (17 or 19) (23 or 29) 2*7*11*17*29 = 75,922

While #01#0 would be (2 or 3) (5 or 7) (11 or 13) (17 or 19) (23 or29) 7*11*29 = 2,233

The benefit of this system would be that wild strings would divide precisely the strings that satisfied them. For whatever that's worth.

...sometimes I wish I had taken some practical programming classes in school :-(.



this is basically what I had in mind but as the string size grows arbitrarily large this becomes impractical. :[
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-05-21 19:20:00
May 21 2011 18:14 GMT
#14
On May 22 2011 02:32 pullarius1 wrote:
I'm not sure how much you get to work with the lists beforehand, but obviously if you could sort the list of wild strings before hand it would help a lot. But it would be a waste of time if the number of strings you were checking for matches for were very low. That is, if you have 100 01# strings, but only needed to find the matches for a few 10 strings, sorting would probably hurt. But if you had 100 strings to match, it would probably be worth your while.

One thing I thought of that is probably not useful at all:
For each string, rehash it into integers in the following way-
For each placenumber i, assign the the 2i-th and (2i-1)th prime to it. If that place number holds a 1 one, choose the odd prime, a 0, choose the even prime, a # choose neither. Multiply all the chosen primes together.

For instance 10110 would be (2 or 3) (5 or 7) (11 or 13) (17 or 19) (23 or 29) 2*7*11*17*29 = 75,922

While #01#0 would be (2 or 3) (5 or 7) (11 or 13) (17 or 19) (23 or29) 7*11*29 = 2,233

The benefit of this system would be that wild strings would divide precisely the strings that satisfied them. For whatever that's worth.

...sometimes I wish I had taken some practical programming classes in school :-(.



well thats actually a great solution, since if message modulo hashed-key = 0 then such an index satisfies the constraint.

So if you map every key by the hash function to this form, and store it in the next slot in the database, as well as with its object pointer, then simply do a linear search for message modulo hashed key = 0 on each element of the array

this circumvents directly hashing onto an array location since that hash function increases faster than n factorial
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2011-05-21 19:16:33
May 21 2011 19:02 GMT
#15
Okay, I gotta re-read it all, cos I'm a bit lost on this one.. .

Edit: okay, I read it! I need to write a sketch over it Might actually work with modulus it with the message.

Your only problem is if the String has 40 wildcards in it - then it takes a long time to write all the possible prime combinations (right?) ... or if you ignore wildcards, is 40x # = 0?

I'm gonna write an algorithm for this rly quick .
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Oracle
Profile Blog Joined May 2007
Canada411 Posts
May 21 2011 19:19 GMT
#16
So when you store an object by its key, create an array with the hashed version of the key (H_i) O(p) and its object pointer O(1). Then store both into the next available position in the dataset O(1).

When you're searching for keys which satisfy a certain message:
First hash the key O(p) = H_k.
Then do a linear check over all database entries such that H_k modulo H_i = 0 and return it. O(l)

p = length of string
l = length of dataset
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2011-05-21 19:34:53
May 21 2011 19:34 GMT
#17
The problem is then, what if the dataset is 5,000,000 strings? :/
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Oracle
Profile Blog Joined May 2007
Canada411 Posts
May 21 2011 19:40 GMT
#18
insertion is O(p)
extraction is O(p+l) in which l will probably dominate p so O(l)

which is still acceptable by any means (l = length of array, so linear time)
5,000,000 wouldn't take an enormous amount of time (in fact 5,000,000 is actually really fast to compute)
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
May 21 2011 19:42 GMT
#19
I'm thinking it might be possible to speed up look up.. Perhaps with tree-search, or other sorting methods.. Ofcourse this would kill the insertion-time.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
haxorz
Profile Blog Joined June 2009
United States138 Posts
May 21 2011 19:50 GMT
#20
^ Yes, it is. I've been thinking about this for the past hour or so and have coded up a working implementation in Java. I'll PM you once I write more tests.
And theres the GG.
1 2 Next All
Please log in or register to reply.
Live Events Refresh
Next event in 2h 28m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Reynor 596
TKL 357
SteadfastSC 85
Rex 73
Railgan 52
mcanning 19
StarCraft: Brood War
Britney 37048
Rain 4457
Horang2 1536
Jaedong 1304
Shuttle 526
Stork 496
Mini 410
BeSt 332
EffOrt 282
firebathero 281
[ Show more ]
Last 173
Leta 136
Barracks 109
PianO 101
ggaemo 89
Hyun 81
Shine 75
LaStScan 69
Shinee 61
JYJ50
Mong 36
sas.Sziky 36
ToSsGirL 26
Movie 23
Bale 21
Rock 20
soO 18
zelot 15
sorry 13
HiyA 12
Sacsri 6
Dota 2
Gorgc5141
qojqva1597
Dendi1101
XcaliburYe169
Other Games
FrodaN5008
B2W.Neo1929
DeMusliM465
Lowko286
Hui .257
Pyrionflax214
Fuzer 207
KnowMe196
oskar98
Organizations
Dota 2
PGL Dota 2 - Main Stream7147
PGL Dota 2 - Secondary Stream1759
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• Berry_CruncH161
• StrangeGG 70
• Kozan
• LaughNgamezSOOP
• AfreecaTV YouTube
• sooper7s
• intothetv
• Migwel
• IndyKCrew
StarCraft: Brood War
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2070
• Ler62
League of Legends
• Nemesis2596
• Stunt908
Other Games
• WagamamaTV167
Upcoming Events
IPSL
2h 28m
ZZZero vs rasowy
Napoleon vs KameZerg
OSC
4h 28m
BSL 21
5h 28m
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
Sparkling Tuna Cup
19h 28m
RSL Revival
19h 28m
Reynor vs sOs
Maru vs Ryung
Kung Fu Cup
21h 28m
Cure vs herO
Reynor vs TBD
WardiTV Korean Royale
21h 28m
BSL 21
1d 5h
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
IPSL
1d 5h
Dewalt vs WolFix
eOnzErG vs Bonyth
Replay Cast
1d 8h
[ Show More ]
Wardi Open
1d 21h
Monday Night Weeklies
2 days
WardiTV Korean Royale
2 days
BSL: GosuLeague
3 days
The PondCast
3 days
Replay Cast
4 days
RSL Revival
4 days
BSL: GosuLeague
5 days
RSL Revival
5 days
WardiTV Korean Royale
5 days
RSL Revival
6 days
WardiTV Korean Royale
6 days
Liquipedia Results

Completed

Proleague 2025-11-14
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
CSCL: Masked Kings S3
SLON Tour Season 2
RSL Revival: Season 3
META Madness #9
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.