Jump to content
  • Advertisement
Sign in to follow this  
swiftcoder

Algorithm to reduce string via rules?

This topic is 2211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
OK, here it is:
 
#include <string>
#include <vector>
#include <iostream>

typedef unsigned long mask;

struct Rule {
  mask i1, i2, o;
 
  Rule(char i1, char i2, char o)
    : i1(1ul<<(i1-'A')),
      i2(1ul<<(i2-'A')),
      o(1ul<<(o-'A')) {
  }
};

std::vector<Rule> rules = {
  Rule{'A','A','A'},
  Rule{'A','A','C'},
  Rule{'C','A','B'},
  Rule{'A','B','B'}
};

void parse(std::string s) {
  int n = s.length();
  mask conversions[n+1][n+1];
 
  for (int i=0; i<n; ++i)
    conversions[i][i+1] = 1ul << (s[i]-'A');
 
 
  for (int l=2; l<=n; ++l) {
    for (int i=0; i<=n-l; ++i) {
      int j = i+l;
      conversions[i][j] = 0ul;
      for (int k = i+1; k < j; ++k) {
        for (Rule rule : rules) {
          if ((rule.i1 & conversions[i][k])
              && (rule.i2 & conversions[k][j]))
            conversions[i][j] |= rule.o;
        }
      }
    }
  }
 
  for (mask m = conversions[0][n], i=0; m; i++, m >>= 1){
    if (m & 1ul)
      std::cout << char('A'+i) << ' ';
  }
  std::cout << '\n';
}

int main() {
  parse("AAAB");
}
 
And now I have some babies to feed. ;)

EDIT: I found the name of the algorithm: CYK. Edited by Álvaro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!