
现给定字符串长度n,与字符串S。接下来是整数q,代表接下来有q次查询。 下面q行有两个整数ai, bi。代表查询特质为ai与bi的用户的匹配度。 1 <= n <= 1000 1 <= q <= 10^6 输入数据全部合法。
每一行输出一个用户匹配度整数。
12 loveornolove 5 3 7 0 0 9 1 3 1 9 5
0 12 3 0 0
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int inf =0x3f3f3f3f;
const double pi = acos(-1.0);
const int N = 1e3 + 10;
int dp[N][N];
char s[N],x[N];
int main()
{
int n, m;
scanf("%d", &n);
scanf("%s", s);
for(int i = 0; i<n; i++)
{
x[n-i-1] = s[i];
}
x[n] = '\0';
memset(dp, 0, sizeof(dp));
for(int i = 1; i<=n; i++)
for(int j = 1; j<=n; j++)
{
if(x[i-1] == x[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else dp[i][j] =0;
}
int u, v;
scanf("%d", &m);
while(m--)
{
scanf("%d%d", &u, &v);
printf("%d\n", dp[n-u][n-v]);
}
return 0;
}
原文:http://blog.csdn.net/dml_96/article/details/51330142