判断回文串
Time Limit: 2000 MS Memory Limit: 4096 KBTotal Submit: 298 Accepted: 50 Page View: 525
Submit Status Discuss
Description
某天吃饭的时候,FM有了一个帅气idea,其实是个傻逼题?题意很简单,给一个字符串判断是否是回文串?
PS:如果一个字符串正着读和反着读都一样,那么这个字符串为回文串,比如aba,cc,cddc为回文串,而ac,acda,adA则不是回文串。
Input
多组输入,第一行为
nn,代表字符串的长度。
第二行为这个字符串,该串仅包含大小写字母。
第二行为这个字符串,该串仅包含大小写字母。
Output
是回文串输出"YES", 不是回文串输出 "NO"。
8
aaaaaaaa
7
tangcan
3
lyl
aaaaaaaa
7
tangcan
3
lyl
YES
NO
YES
NO
YES
Hint
回文串长度
1≤len≤1071≤len≤107,请注意内存限制。
题意:中文题目不解释
思路:1e7的长度,4M的内存限制,别幻想开数组暴力了,正解为hash,先hash前面一半的再hash后面的那一半然后判断前后hash值是否相等,据说洋哥卡掉了单hash,要用双hash,不过我比较头铁单双hash都尝试了,居然都过了,可能是我用的mod很少见,没有被卡掉,所以在学习hash的时候要用别人很少用的mod和base值,这样出题人就很难卡你了。hash很强大,可以深入学习哈
代码:
#include<stdio.h> #include<string.h> #include<cmath> #include<stdlib.h> #include<time.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> #define ll long long #define ull unsigned long long #define qq printf("QAQ\n"); using namespace std; const int maxn=1e5+5; const int inf=0x3f3f3f3f; const ll mod=1e9+7; const ull hmod1=1e9+97; const ull hmod2=1e9+93; const ull base1=133; const ull base2=131; const double e=exp(1.0); const double pi=acos(-1); int main() { int n; ll a,b,c,d,b1,b2; while(scanf("%d",&n)!=EOF) { char cc; a=0,b=0; getchar(); for(int i=1;i<=n/2;i++) { cc=getchar(); a=(a*base1+(ull)cc)%hmod1; b=(b*base2+(ull)cc)%hmod2; } if(n&1)getchar(); b1=1,b2=1,c=0,d=0; for(int i=1;i<=n/2;i++) { cc=getchar(); c=(c+(ull)cc*b1)%hmod1; d=(d+(ull)cc*b2)%hmod2; b1=(b1*base1)%hmod1; b2=(b2*base2)%hmod2; } //if(a==c)printf("YES\n"); if(a==c&&b==d)printf("YES\n"); else printf("NO\n"); } return 0; }