-
Notifications
You must be signed in to change notification settings - Fork 0
/
sq-subseq.cpp
71 lines (43 loc) · 1.02 KB
/
sq-subseq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
long results[50][50][50][50];
string s;
int size;
long f(int i, int j , int k, int l){
if(k>l || i>j || j>=k) return 0;
if(results[i][j][k][l] != -1) return results[i][j][k][l];
long sum = 0;
string temp = s.substr(k,l);
for(int h=i;h<=j;h++){
int last = temp.find_last_of(s[h]);
if(last == -1) continue;
sum = (sum + 1 + f(i,h,last+1,l))%1000000007;
}
results[i][j][k][l] = sum;
return results[i][j][k][l];
}
int main(){
int T;
cin>>T;
long sum1;
while(T-->0){
cin>>s;
sum1 = 0;
size = s.length();
for(int i=0;i<size;i++)
for(int j=i;j<size;j++)
for(int k=j;k<size;k++)
for(int l=k;l<size;l++)
results[i][j][k][l] = -1;
for(int i=0; i<size-1; i++){
int last = s.find_last_of(s[i]);
if(last==i) continue;
cout<<last<<" : "<<s[i]<<endl;
sum1 = (sum1 + 1 + f(0 , i-1 , i+1 , last-1))%1000000007;
}
cout<<sum1<<endl;
}
return 0;
}