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