预处理所有26种变化的hash表,用hash表来判断子串是否相等
假设x的第i种变化与y相等,ans=min(i,26-i)
都不相等就为-1
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
const int maxn =1e5+5;
const int maxm=10000;
const int mod =1e9+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
char ch[maxn];
char sh[30][maxn];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",ch);
for(int i = 0 ; i < 26 ; i ++)
{
for(int j = 0 ; j < n ; j++) sh[i][j] = (ch[j] - 'a' + i) % 26 + 'a';
}
int q;
scanf("%d",&q);
while(q--)
{
int x,y,len,flag = 0;
scanf("%d%d%d",&x,&y,&len);
x--;y--;
for(int i = 0 ; i < 26 ; i++)
{
if(strncmp(ch+y,sh[i] + x,len) == 0)
{
flag = 1;
printf("%d\n",min(i,26-i));
break;
}
}
if(!flag) puts("-1");
}
}