题目链接https://www.oj.swust.edu.cn:50443/problem/show/2610
Description
某天吃饭的时候,FM有了一个帅气idea,其实是个傻逼题?题意很简单,给一个字符串判断是否是回文串?
PS:如果一个字符串正着读和反着读都一样,那么这个字符串为回文串,比如aba,cc,cddc为回文串,而ac,acda,adA则不是回文串。
Input
多组输入,第一行为n,代表字符串的长度。第二行为这个字符串,该串仅包含大小写字母。
Output
是回文串输出"YES", 不是回文串输出 “NO”。
Sample Input
8
aaaaaaaa
7
tangcan
3
lyl
Sample Output
YES
NO
YES
Hint
回文串长度 1≤len≤107,请注意内存限制。Memory Limit: 4096 KB
正反求hash值即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
//#pragma GCC optimize(3,"Ofast","inline")
#define LL long long
//#define MT(a,b) memset(a,b,sizeof(a))
const int mod=1000007;
const int maxn=2e4+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
unsigned LL Hash1,Hash2,p=233,P;
int main (){
int n;
char word;
while (~scanf("%d",&n)){
getchar();
int mid=n>>1;
Hash1=Hash2=0;
for (int i=1;i<=mid;i++){
word=getchar();
Hash1=(Hash1*p+(unsigned LL)word)%mod;
}
if (n&1) getchar();
P=1;
for (int i=1;i<=mid;i++){
word=getchar();
Hash2=(Hash2+(unsigned LL)word*P)%mod;
P=(P*p)%mod;
}
if (Hash1==Hash2) printf("YES\n");
else printf("NO\n");
}
return 0;
}