# Square Subsequences

### Problem Statement :

```Square Subsequences

A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.

Input Format

The first line contains the number of test cases, T.
T test cases follow. Each case contains a string, S.

Output Format

Output T lines, one for each test case, containing the required answer modulo 1000000007.

Constraints:
1 <= T <= 20
S will have at most 200 lowercase characters ('a' - 'z').```

### Solution :

```                            ```Solution in C :

In C++ :

#include<iostream>
#include<set>
#include<map>
#include<string>
#include<stdio.h>
#include<sstream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<string.h>
using namespace std ;
#define MAXN 305
#define INF (int)1e9
#define MOD 1000000007

int brute(string s)
{
int n = s.size() ;
int ret = 0 ;
for(int i = 1;i < 1 << n;i++)
{
string t ;
for(int j = 0;j < n;j++)
if(i & 1 << j)
t.push_back(s[j]) ;
if(t.size() % 2 == 1) continue ;
int tt = t.size() ;
bool valid = true ;
for(int j = 0;j < tt / 2;j++)
if(t[j] != t[tt / 2 + j])
valid = false ;
if(valid) ret++ ;
}
return ret ;
}

string s ;
int n ;
int start1,start2 ;

int memo[MAXN][MAXN] ;
int solve(int k1,int k2)
{
if(k1 == start2 || k2 == n) return 0 ;
int ret = s[k1] == s[k2] ? 1 : 0 ;
if(memo[k1][k2] != -1) return memo[k1][k2] ;
ret += solve(k1 + 1,k2) ;
ret += solve(k1,k2 + 1) ;
if(ret >= MOD) ret -= MOD ;
ret -= solve(k1 + 1,k2 + 1) ;
if(ret < 0) ret += MOD ;
if(s[k1] == s[k2]) ret += solve(k1 + 1,k2 + 1) ;
if(ret >= MOD) ret -= MOD ;
return memo[k1][k2] = ret ;
}

int solve(string _s)
{
s = _s ;
n = s.size() ;
int ret = 0 ;
for(start2 = 0;start2 < n;start2++)
{
memset(memo,255,sizeof memo) ;
for(start1 = 0;start1 < start2;start1++)
if(s[start1] == s[start2])
{
int cret = 1 + solve(start1 + 1,start2 + 1) ;
ret += cret ;
if(ret >= MOD) ret -= MOD ;
}
}
return ret ;
}

void test()
{
for(int test = 0;test < 10000;test++)
{
int n = rand() % 10 + 1 ;
string t ;
for(int j = 0;j < n;j++) t.push_back('a' + rand() % 2) ;
int ret1 = brute(t) ;
int ret2 = solve(t) ;
cout << ret1 << " " << ret2 << endl ;
if(ret1 != ret2)
{
cout << "Failed on: " << test << endl ;
cout << t << endl ;
while(1) ;
}
}
}

void generate()
{
char in[10] = "in .txt" ;
for(int test = 0;test < 10;test++)
{
in[2] = test + '0' ;
FILE * fout = fopen(in,"w") ;

int runs = 20 ;
fprintf(fout,"%d\n",runs) ;
for(int t = 0;t < runs;t++)
{
string c ;
int n = rand() % 200 + 1 ;
if(test < 2) n = rand() % 20 + 1 ;

if(test < 4) for(int i = 0;i < n;i++) c.push_back(rand() % 26 + 'a') ;
else if(test < 7) for(int i = 0;i < n;i++) c.push_back(rand() % 3 + 'a') ;
else if(test < 10) for(int i = 0;i < n;i++) c.push_back(rand() % 2 + 'a') ;

fprintf(fout,"%s\n",c.c_str()) ;
}
}
}

int main()
{
// test() ; return 0 ;
// generate() ; return 0 ;

int runs ;
cin >> runs ;
while(runs--)
{
string s ;
cin >> s ;
int ret = solve(s) ;
cout << ret << endl ;
}
return 0 ;
}

In Java :

import java.io.*;
import java.util.*;

public class Solution implements Runnable {
final static int MOD = 1000000007;

int count(String a, String b) {
int n = a.length();
int m = b.length();
int dp[][] = new int[n + 1][m + 1];
int sum[][] = new int[n + 1][m + 1];
for (int i = 0; i <= n; ++ i) {
sum[i][m] = 1;
}
for (int j = 0; j <= m; ++ j) {
sum[n][j] = 1;
}
for (int i = n - 1; i >= 0; -- i) {
for (int j = m - 1; j >= 0; -- j) {
if (a.charAt(i) == b.charAt(j)) {
dp[i][j] = sum[i + 1][j + 1];
}
sum[i][j] = (sum[i + 1][j] + sum[i][j + 1]
- sum[i + 1][j + 1] + dp[i][j]) % MOD;
}
}
int result = 0;
for (int i = 0; i < n; ++ i) {
result += dp[i][0];
result %= MOD;
}
return result;
}

int count(String s) {
int n = s.length();
int result = 0;
for (int i = 1; i < n; ++ i) {
result += count(s.substring(0, i), s.substring(i, n));
result %= MOD;
}
return (result + MOD) % MOD;
}

public void run() {
try {
while (testCount > 0) {
testCount --;
}
} catch (Exception e) {
}
}

public static void main(String args[]) {
}
}

In C :

#include <stdio.h>

#define P 1000000007

char s[500];
long long i,j,k,l,m,n,t,kk;
//a[210][210][210],
long long b[210][210][210];

int main()
{

scanf("%lld\n",&t);
while(t--)
{
scanf("%s\n",s);

//printf("%s %lld",s,n);

n=0;
while(s[n]) n++;

//printf("%s %lld\n",s,n);

for(j=0;j<=n;j++)
for(i=j;i<=n;i++)
for(k=i;k<=n;k++) b[j][i][k]=0;

m=0;

for(k=n-1;k>=0;k--)
for(i=k-1;i>=0;i--)
for(j=i;j>=0;j--)
{
b[j][i][k] = 0;
//b[j+1][i][k];

if(s[j]==s[k])
{
// a[j][i][k] = (1+b[j+1][i][k+1])%P;
if(i+1==k) m = (m+1+b[j+1][i][k+1])%P;

b[j][i][k] = (b[j][i][k] +1 + b[j+1][i][k+1])%P;
//           printf("%c %lld %lld b=%lld\n",s[j],j,k,b[j][i][k]);
}
b[j][i][k] = (b[j][i][k]+b[j][i][k+1]+b[j+1][i][k]-b[j+1][i][k+1]);

l++;
}

//printf("%lld=l\n",l);

//m=0;

//for(i=0;i<n;i++)
// for(k=i+1;k<n;k++) m+= a[i][k-1][k];

printf("%lld\n",m);

//printf("%lld %lld=n\n",b[0][13][14],n);
//printf("%lld %lld=n\n",b[4][14][15],n);

}

return 0;
}

In Python3 :

from sys import stderr

def dp(a,b):
c = [[0 for j in range(len(b))] for i in range(len(a))]
for i in range(len(b)):
if a[0] == b[i]:
c[0][i] = 1
if i:
c[0][i] += c[0][i-1]
c[0][i] %= mod
for i in range(1,len(a)):
c[i][0] = c[i-1][0]
for j in range(1,len(b)):
c[i][j] = c[i-1][j] + c[i][j-1] - c[i-1][j-1]
if a[i] == b[j]:
c[i][j] += c[i-1][j-1]
c[i][j] %= mod
return c[len(a)-1][len(b)-1]

def backtrack(c,a,b,i,j):
if i == 0 or j == 0:
return ''
elif a[i-1] == b[j-1]:
return backtrack(c,a,b,i-1,j-1) + a[i-1]
else:
if c[i][j-1] > c[i-1][j]:
return backtrack(c,a,b,i,j-1)
else:
return backtrack(c,a,b,i-1,j)

mod = 1000000007

t = int(input().strip())

for _ in ' '*t:
s = list(input().strip())
s = ''.join(x for x in s if s.count(x) > 1)
c = 0

for k in range(1,len(s)):
d = dp(s[k:],s[:k])
print(s[:k],s[k:],file=stderr)
print(d,file=stderr)
c += d
c %= mod

print(c)```
```

## View More Similar Problems

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

## Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

## Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie