hackerrank:Palindrome Index(hash)
2015-08-28 15:18
387 查看
题目连接:Palindrome Index
Problem Statement
You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution.
In case the string is already a palindrome, then
also a valid answer along with possible indices.
Input Format
The first line contains T,
i.e. the number of test cases.
T lines
follow, each containing a string.
Output Format
Print the position (0 index) of the letter by removing which the string turns into a palindrome. For a string, such as
we can remove b at index 0 or c at
index 3. Both answers are accepted.
Constraints
1≤T≤20
1≤ length
of string ≤100005
All characters are Latin lower case indexed.
Sample Input
Sample Output
Explanation
In the given input, T = 3,
For input aaab, we can see that
removing b from the string makes
the string a palindrome, hence the position 3.
For input baa, removing b from
the string makes the string palindrome, hence the position 0.
As the string aaa is already a palindrome,
you can output 0, 1 or 2 as removal of any of the characters still maintains the palindrome property. Or you can print -1 as this is already a palindrome.
题意:问你从某个位置删除一个字符后剩余的回文
分析:直接字符串hash,但是要判断奇偶啥的,比较麻烦
Problem Statement
You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution.
In case the string is already a palindrome, then
-1is
also a valid answer along with possible indices.
Input Format
The first line contains T,
i.e. the number of test cases.
T lines
follow, each containing a string.
Output Format
Print the position (0 index) of the letter by removing which the string turns into a palindrome. For a string, such as
[code]bcbc
we can remove b at index 0 or c at
index 3. Both answers are accepted.
Constraints
1≤T≤20
1≤ length
of string ≤100005
All characters are Latin lower case indexed.
Sample Input
[code]3 aaab baa aaa
Sample Output
[code]3 0 -1
Explanation
In the given input, T = 3,
For input aaab, we can see that
removing b from the string makes
the string a palindrome, hence the position 3.
For input baa, removing b from
the string makes the string palindrome, hence the position 0.
As the string aaa is already a palindrome,
you can output 0, 1 or 2 as removal of any of the characters still maintains the palindrome property. Or you can print -1 as this is already a palindrome.
题意:问你从某个位置删除一个字符后剩余的回文
分析:直接字符串hash,但是要判断奇偶啥的,比较麻烦
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int maxn=1e5+100; const int p=1e6+100; const int mod=1e9+7; int ha1[maxn+100],ha2[maxn+100]; int t,len; char str[maxn]; void Hash() { for(int i=1;i<=len;i++) ha1[i]=(ha1[i-1]*1LL*p+str[i])%mod; for(int i=len;i>=1;i--) ha2[i]=(ha2[i+1]*1LL*p+str[i])%mod; } LL Pow(LL a,LL b) { if(b<0) return 0; LL ans=1; while(b) { if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return ans; } int gethash1(int l,int r) { if(l>r) return 0; return (ha1[r]-ha1[l-1]*1LL*Pow(p,r-l+1)%mod+mod)%mod; } int gethash2(int l,int r) { if(r>l) return 0; return (ha2[r]-ha2[l+1]*1LL*Pow(p,l-r+1)%mod+mod)%mod; } int main() { scanf("%d",&t); while(t--) { scanf("%s",str+1); len=strlen(str+1); ha1[0]=ha2[0]=ha1[len+1]=ha2[len+1]=0; Hash(); int mid=(len+1)/2,pos=-1; if((len&1)&&(ha1[mid-1]==ha2[mid+1])) puts("-1"); else if(ha1[mid]==ha2[mid+1]) puts("-1"); else { if(len&1) { //cout<<"wocao "<<endl; for(int i=1;i<mid;i++) { int s1=ha1[i-1]; int s2=gethash1(i+1,mid); int s=(s1*1LL*Pow(p,mid-i)%mod+s2)%mod; if(s==gethash2(len,mid+1)) { pos=i; break; } } if(pos==-1) { for(int i=mid+1;i<=len;i++) { int s1=ha2[i+1]; int s2=gethash2(i-1,mid); int s=(s1*1LL*Pow(p,i-mid)%mod+s2)%mod; if(s==gethash1(1,mid-1)) { pos=i; break; } } } } else { for(int i=1;i<=mid;i++) { int s1=ha1[i-1]; int s2=gethash1(i+1,mid); int s=(s1*1LL*Pow(p,mid-i)%mod+s2)%mod; if(s==gethash2(len,mid+2)) { pos=i; break; } } if(pos==-1) { for(int i=mid+1;i<=len;i++) { int s1=ha2[i+1]; int s2=gethash2(i-1,mid+1); int s=(s1*1LL*Pow(p,i-mid-1)%mod+s2)%mod; if(s==gethash1(1,mid-1)) { pos=i; break; } } } } printf("%d\n",pos-1); } } return 0; }
相关文章推荐
- java源码查看工具之Source Insight
- CSS3:固定textarea文本域宽度
- 手机网站和电脑网站设计是需注意的区别
- 3.仓库
- Qt Creator编译器设置:添加C++11支持
- Activiti值活动节点,排他网关(ExclusiveGateWay)and并行网关(parallelGateWay) (八)
- 匈牙利算法
- 最长递增子序列 O nlgn时间复杂度
- Entity Framework Extended Library
- CSS样式表
- GridView使用CommandField删除列实现删除时提示确认框
- Android 安装错误汇总
- roboming前端开发统一规范
- Nginx日志处理
- Android -HandleThread-给线程传参数
- JavaScript:this是什么?
- GB/T 1.1—2009《标准化工作导则 第1部分:标准的结构和编写》简介
- [SCOI2010][BZOJ1854] 游戏|二分图匹配|匈牙利算法|并查集
- C# ZedGraph 控件各属性以及示例
- GridView使用CommandField删除列实现删除时提示确认框