UvaLive 6439 Pasti Pas! 字符串哈希
来源:互联网 发布:网络推广证书 编辑:程序博客网 时间:2024/06/06 10:55
链接:http://vjudge.net/problem/viewProblem.action?id=47586
题意:给一个字符串,可以将从前数第i~j和从后数第i~j字符串看作一个字符,问整段字符串看作一个回文里有多少个字符。
思路:字符串哈希,从前开始哈希也从后开始哈希,遇到哈希值相同就多两个字符,最后处理一下中间的字符即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <cstdlib>#include <queue>#include <stack>#include <vector>#include <ctype.h>#include <algorithm>#include <string>#include <set>#include <ctime>#define PI acos(-1.0)#define maxn 10005#define INF 0x7fffffff#define eps 1e-8#define seed 31typedef long long LL;typedef unsigned long long ULL;using namespace std;ULL aa[50005];void init(){ aa[0]=1; for(int i=1;i<=50000;i++) aa[i]=aa[i-1]*seed;}int main(){ int T; char ss[50005]; scanf("%d",&T); init(); for(int ii=1;ii<=T;ii++) { ULL head=0,tail=0; scanf("%s",ss); printf("Case #%d: ",ii); //cout<<endl; int l=strlen(ss); int k=l/2; int from=0; int ans=0; for(int i=0;i<k;i++) { //cout<<i<<" "<<l-i-1<<endl; //cout<<head<<" "<<tail<<endl; head*=seed; head+=(ss[i]-'A'+1); tail+=(ss[l-i-1]-'A'+1)*aa[i-from]; //cout<<i<<" "<<from<<endl; //cout<<head<<" "<<tail<<endl; if(head==tail) { ans+=2; from=i+1; head=0; tail=0; } } if(head!=0) ans++; if(l%2==1&&head==0) ans++; printf("%d\n",ans); } return 0;}
0 0
- UvaLive 6439 Pasti Pas! 字符串哈希
- UValive 6439 - Pasti Pas!
- UVALive 6439--Pasti Pas!
- UVALive 6439 Pasti Pas!
- Uvalive6439(Pasti Pas!)
- UVALive 6047 Perfect Matching(字符串哈希)#by zh
- pas
- UVALive 7370--字符串排序
- 【字符串处理】UVALive
- UVALive 6439
- UVALive 6085|Chemistry|字符串处理
- UVALive 4174 Steganography 字符串处理 密码学
- uvalive 5734 字符串最小表示法
- UVALive 2755 Hidden Password(字符串最小表示)
- UVALive 4167 Parity (字符串水)
- Uvalive 4513 Stammering Aliens(字符串Hash)
- UVALive
- UVALive
- Fibonacci 数列
- poj 百练 2765 八进制小数(精度问题)
- C# 中的委托和事件
- 诺基亚的失败错在善待员工,有这样的错吗
- 基于TCP协议的服务器/客户端程序
- UvaLive 6439 Pasti Pas! 字符串哈希
- JAX-RS:开发一个简单的服务
- PHP
- 基于UDP协议的服务器/客户端程序
- 关机、自启动、全屏、屏蔽任务管理器
- EHCache的使用
- poj_2352树状数组
- UML之序列图(Sequence Diagram)
- Android AsyncHttpclient 使用详解