Palindromic Border

Sort by

recency

|

41 Discussions

|

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Palindromic Border Problem Solution

  • + 0 comments

    Here is the solution of Palindromic Border Click Here

  • + 1 comment

    https://zeroplusfour.com/palindromic-border/

    Here's how I did in all languages Java 8 , C++ , C , Python 3, Python 2.

  • + 1 comment

    Python3 solution

    def mod(x):
        return x % 1000000007
    
    class trie(object):
        def __init__(self, depth=0, parent=None, data=None):
            self.count = 0
            self.depth = depth
            self.parent = parent
            self.data = data
            self.next = {}
            
        def get(self, char):
            if char in self.next:
                return self.next[char]
            ans = trie(self.depth + 2, self, char)
            self.next[char] = ans
            return ans
        
        def children(self):
            return self.next.values()
    
    def dfs(node):
        ans = 0
        stack = []
        stack.append((node, True))
        while len(stack) > 0:
            node, extend = stack.pop()
            if extend:
                stack.append((node, False))
                for child in node.children():
                    stack.append((child, True))
            else:
                for child in node.children():
                    node.count += child.count
                if node.depth > 0:
                    ans = mod(ans + mod(node.count * (node.count - 1) // 2))
        return ans
    
    def pr(node):
        print(' ' * node.depth + '%s (%d:%d)' % (chr(node.data) if node.data else ' ', node.depth, node.count))
        for child in node.children():
            pr(child)
    
    def score(string):
        # ascii values
        string = list(map(ord, string))
        N = len(string)*2 + 1
        c = [0] * N
        c[1::2] = string
        # initialize trie pointers
        odd = trie(depth = -1)
        even = trie()
        center, radius = 0, 0
        left, right = 0, 0
        # manacher's algorithm
        node = [even]
        span = [0]
        for i in range(1, N):
            if i > radius:
                node.append(odd.get(c[i]) if (i & 1) else even)
                span.append(0)
                left = i - 1
                right = i + 1
            else:
                mirror = center * 2 - i
                if span[mirror] < radius - i:
                    span.append(span[mirror])
                    node.append(node[mirror])
                    left = -1
                else:
                    span.append(radius - i)
                    node.append(node[mirror])
                    while node[i].depth > radius - i:
                        node[i] = node[i].parent
                    right = radius + 1
                    left = i * 2 - right
            while left >= 0 and right < N and c[left] == c[right]:
                if c[right]:
                    node[i] = node[i].get(c[right])
                span[i] += 1
                left -= 1
                right += 1
            node[i].count += 1
            if i + span[i] > radius:
                center = i
                radius = i + span[i]
        # accumulate count
        return mod(dfs(odd) + dfs(even))
        
    print(score(str(input())))
    
  • + 0 comments
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    
    typedef long long ll;
    #define FOR(i, a, b) for (int i = (a); i < (b); i++)
    #define ROF(i, a, b) for (int i = (b); --i >= (a); )
    
    int ri()
    {
      int x;
      scanf("%d", &x);
      return x;
    }
    
    const int N = 100000;
    char a[N+1];
    struct Node { int suff, l, c[26], cnt; } b[N+2];
    
    int getSuff(int i, int x)
    {
      while (i-1-b[x].l < 0 || a[i-1-b[x].l] != a[i])
        x = b[x].suff;
      return x;
    }
    
    int main()
    {
      b[0].suff = 1; b[0].l = 0;
      b[1].suff = 1; b[1].l = -1;
      scanf("%s", a);
      int x = 1, y = 2;
      for (int i = 0; a[i]; i++) {
        x = getSuff(i, x);
        if (! b[x].c[a[i]-'a']) {
          b[y].l = b[x].l+2;
          b[y].suff = b[getSuff(i, b[x].suff)].c[a[i]-'a'];
          b[y].cnt = 0;
          b[x].c[a[i]-'a'] = y++;
        }
        x = b[x].c[a[i]-'a'];
        b[x].cnt++;
      }
      ROF(i, 0, y)
        b[b[i].suff].cnt += b[i].cnt;
      ll ans = 0;
      FOR(i, 2, y) {
        int c = b[i].cnt;
        ans += ll(c-1)*c/2;
      }
      printf("%lld\n", ans%1000000007);
    }