Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String reconstruction

 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, folks

Need some ideas on how you will do this:

receive a string, and then chop it into 3 pieces, use any of the 2 resultant strings, it should be able to restore the original string.

Thanks
 
Jess Ryan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the most powerful way of approaching this (in my opinion) would be to use java.util.regex.Pattern and java.util.regex.Matcher. With these classes, you can create patterns to search and/or parse a string just about any way you can think of.

Another approach would be to use java.util.StringTokenizer, but this class is going out of style and its use is discouraged in new code.
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jess Finley:
Well, the most powerful way of approaching this (in my opinion) would be to use java.util.regex.Pattern and java.util.regex.Matcher. With these classes, you can create patterns to search and/or parse a string just about any way you can think of.

Another approach would be to use java.util.StringTokenizer, but this class is going out of style and its use is discouraged in new code.

Hi Jess, thanks for your reply, but there is no pattern, I need to slice the string 3 parts evenly, and then do some "Magic" so that with any of the 2 parts out of 3, I will be able to restore the original string,
 
Jess Ryan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh, well, I don't understand the problem... specifically the "Magic" I don't see how you can leave 1/3 of the string behind and still have the whole string. But, you can find out the input string's length (String.length()) and divide that in to three parts, for starters...
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with an int length = string.length(), do some arithmetic then use substring(int,int) method of the string class to allocate three StringBuffers, which is a class right next to String in the documentaion.

StringBuffer class has ways of doing what you want. String class is resistant to chop it into 3 pieces
[ March 01, 2008: Message edited by: Nicholas Jordan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Nick]: StringBuffer class has ways of doing what you want. String class is resistant to chop it into 3 pieces

Not really. It's pretty easy to create three pieces using the substring() method, with no StringBuffers anywhere.

The real problem here is the stuff Jess has been asking about: how can we reconstruct the original string using only 2 of the three pieces?

Hongli Li, let's take an example. If the original string is "abcdefghi", we can split that into three parts, "abc", "def", "ghi". Is that what you mean? OK, then if we take just two of those, "abc" and "def", how can we reconstruct the original string? If we don't know anything about "ghi", how can we recreate that data?

I have the idea that this may actually be a much more complex problem, where we don't really split the original string, but create three different smaller strings (not substrings) from which it is possible to reconstruct the original string. Perhaps the goal is to find the smallest set of strings for which this is possible, or to ensure that it is not possible to recover the original string from only 1 of the three strings, but it is always possible using any 2 of the three strings. I think you would really need to get a better idea of what the requirements really are before spending too much time on it. The problem I'm thinking of would definitely not be a beginner's problem though. It's probably more of a cryptography problem.
[ March 01, 2008: Message edited by: Jim Yingst ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jim:]It's probably more of a cryptography problem.
Yes, that is why I suggested StringBuffer - the one time I looked at it the intent seemed ( from memory ) to derive from the need to manipulate Strings. I have been studying crypto for several months now and that is why I tried to simplifiy it. Crypto and beginner area do not mix, I have bruises to show for it. The words of original poster: need to slice the string 3 parts evenly, and then do some "Magic" so that with any of the 2 parts out of 3, I will be able to restore the original string so if is XOR I imagine Hongli Li could byte[] array = string.getBytes(); which yields a byte array and is what they seem to use in encipherment when they want to do magic. Since our honored master (has) the idea that this may actually be a much more complex problem ..., I suggest we invite the original poster to Security where complex work is much more common and well received.
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a vintage thread, back to the days I even couldn't elaborate myself well, it's a cryptography problem and I solved it with Shamir's Secret Sharing.
Thanks guys even thought it's too late to say it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the solution. We have a saying in Britain: "better late than never."
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Thank you for the solution. We have a saying in Britain: "better late than never."


Yeah, nice to know the precious saying and It feels good to share the knowledge.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic