牛客練習賽33 E tokitsukaze and Similar String
阿新 • • 發佈:2018-12-15
預處理所有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"); } }