jeudi 26 janvier 2017

Converting a hex fraction

I am working on a program and I am stuck on the last part. Given a fraction in hex, for instance, 1/17, should produce the following: 0.0B21642C859. I have my program working up to this point in that I can convert a hex fraction. I am stuck on how to go about finding repeating patterns in the resulting string. For instance 1/6 produces 0.2AAAAAA, so I have the string 2AAAAAA. How can a search for patterns in the string such that I can parse the non-repeating part from the recurring portion? I thought maybe using the longest repeated substring technique might work. I have a simple method that should return the length of the longest substring:

 int lengthOfSub(string s) {
    int len = s.length();
    int i = 0;
    int j = 0;
    int biggestSoFar = 1;
    bool matches[256] = { false };
    while (j < len) {
       if (matches[s[j]]) {
          biggestSoFar = max(biggestSoFar, j-i);
          while (s[i] != s[j]) {
          matches[s[i]] = false;
          i++;
       }
      i++;
      j++;
   }  else {
     matches[s[j]] = true;
      j++;
    }
  }
  biggestSoFar = max(biggestSoFar, len-i);
  return biggestSoFar;
}

However, I get some unexpected behaviour. If I pass it the string 0A3D70A3D70A4 it returns length of 6 instead of 4, which I would expect since the pattern is 0A3D7. Why is the method not returning the correct length?

Aucun commentaire:

Enregistrer un commentaire