标签:记忆化搜索
1 4 abab
6
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 1e5 * 2 + 100;
char s[M];
int map[M];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
memset(map,0,sizeof(map));
scanf("%d %s",&n,s);
int tp,len,ans;
len = strlen(s);
map[0] = ans = 0;
tp = 1;
for(int i = 1; s[i]; i++)
if(s[i] == s[0]) map[tp++] = i;
ans += tp;
for(map[0]++; map[0] < len; map[0]++)
{
int temp = 1;
for(int i = 1; i < tp; i++)
if(s[map[i] + 1] == s[map[0]]) map[temp++] = map[i] + 1;
tp = temp;
ans += tp;
if(ans > 10007) ans %= 10007;
}
printf("%d\n",ans);
}
return 0;
}
标签:记忆化搜索
原文地址:http://blog.csdn.net/zsgg_acm/article/details/39642145