1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
U322188 ┼╨╢╧╗╪╬─╫╙┤о.cpp 932 Bytes
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#include <iostream>
#include <string>
#define B 131
using namespace std;
int main(){
string s;
cin>>s;
unsigned long long h1[s.length()],h2[s.length()];
h1[0] = s[0];
h2[s.length()-1] = s[s.length()-1];
unsigned long long powb[s.length()+1];
powb[0] = 1;
powb[1] = B;
for(int i=1;i<s.length();i++){
powb[i+1] = powb[i]*B;
h1[i] = h1[i-1]*B+s[i];
h2[s.length()-i-1] = h2[s.length()-i]*B+s[s.length()-i-1];
}
int q;
cin>>q;
for(int t=0;t<q;t++){
int l,r;
cin>>l>>r;
// if(l == r){
// cout<<"YES"<<endl;
// continue;
// }
l--;
r--;
int a,b;
if(l == 0)
a = h1[l+(r-l+1)/2];
else
a = h1[l+(r-l+1)/2]-h1[l-1]*powb[(r-l+1)/2+1];
if(r == s.length()-1)
b = h2[r-(r-l+1)/2];
else
b = h2[r-(r-l+1)/2]-h2[r+1]*powb[(r-l+1)/2+1];
if(a == b)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385