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 {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
int testCount = Integer.parseInt(reader.readLine());
while (testCount > 0) {
testCount --;
System.out.println(count(reader.readLine()));
}
} catch (Exception e) {
}
}
public static void main(String args[]) {
new Thread(new Solution()).run();
}
}
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
Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →