Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs @ 11857

Last change on this file since 11857 was 11857, checked in by gkronber, 9 years ago

#2283: solution reorg

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6
7namespace HeuristicLab.Problems.GrammaticalOptimization {
8  public class PalindromeProblem : ISymbolicExpressionTreeProblem {
9    // length of the longest palindrome in the sentence
10    private const string grammarString = @"
11G(S):
12S -> T | TS
13T -> a .. z
14";
15
16
17    private const string hlGrammarString = @"
18G(S):
19S -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | SS
20";
21
22    private readonly IGrammar grammar;
23    public PalindromeProblem() {
24      this.grammar = new Grammar(grammarString);
25      this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
26    }
27
28    public double BestKnownQuality(int maxLen) {
29      // the whole sentence is a palindrome
30      return maxLen;
31    }
32
33    public IGrammar Grammar {
34      get { return grammar; }
35    }
36
37    public double Evaluate(string sentence) {
38      return LongestPalindromicSubstring(sentence).Length;
39    }
40
41
42    private static string Preprocess(string s) {
43      var t = new char[2 * s.Length + 3];
44      t[0] = '$';
45      t[t.Length - 2] = '#';
46      t[t.Length - 1] = '@';
47      for (int i = 0; i < s.Length; i++) {
48        t[2 * (i) + 1] = '#';
49        t[2 * i + 2] = s[i];
50      }
51
52      return new string(t);
53    }
54
55    public static string LongestPalindromicSubstring(string str) {
56      var tLen = 2 * str.Length + 3;
57      var p = new int[tLen];
58      var t = Preprocess(str);
59
60      var center = 0;
61      var right = 0;
62      for (int i = 1; i < t.Length - 1; i++) {
63        var mirror = 2 * center - i;
64        if (right > i) p[i] = Math.Min(right - i, p[mirror]);
65        while (t[i + (1 + p[i])] == t[i - (1 + p[i])]) {
66          p[i]++;
67        }
68        if (i + p[i] > right) {
69          center = i;
70          right = i + p[i];
71        }
72      }
73      var len = 0;
74      center = 0;
75      for (int i = 0; i < t.Length - 1; i++) {
76        if (p[i] > len) {
77          center = i;
78          len = p[i];
79        }
80      }
81
82      var start = (center - 1 - len) / 2;
83      var result = new StringBuilder();
84      for (int i = 0; i < len; i++) {
85        result.Append(str[start + i]);
86      }
87      return result.ToString();
88    }
89
90    public string CanonicalRepresentation(string phrase) {
91      return phrase;
92    }
93
94    public IEnumerable<Feature> GetFeatures(string phrase) {
95      throw new NotImplementedException();
96    }
97
98    public IGrammar TreeBasedGPGrammar { get; private set; }
99    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
100      var sb = new StringBuilder();
101      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
102        if (s.Symbol.Name == "S") continue;
103        sb.Append(s.Symbol.Name);
104      }
105      return sb.ToString();
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.