Coderforces 7D hash+dp
2016-10-04 19:58
253 查看
点击打开链接
题意:题意比较简单不说了
思路:用hash来判断一下到当前位置是不是回文串,利用hash来写真的是很方便,然后一个(看似)简单的DP就OK了#include <map>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=5000010;
char str[maxn];
ull Hash[maxn],base[maxn],p=133331,A[maxn],B[maxn];
ll dp[maxn];
int main(){
base[1]=1;for(int i=2;i<maxn;i++) base[i]=base[i-1]*p;
while(scanf("%s",str+1)!=-1){
int len=strlen(str+1);
A[0]=0;
for(int i=1;i<=len;i++) A[i]=A[i-1]*p+str[i]-'a'+1;
B[0]=0;
for(int i=1;i<=len;i++) B[i]=B[i-1]+(str[i]-'a'+1)*base[i];
ll ans=0;
for(int i=1;i<=len;i++){
if(A[i]==B[i]){
dp[i]=dp[i>>1]+1;
ans+=dp[i];
}
}
printf("%I64d\n",ans);
}
return 0;
}
题意:题意比较简单不说了
思路:用hash来判断一下到当前位置是不是回文串,利用hash来写真的是很方便,然后一个(看似)简单的DP就OK了#include <map>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=5000010;
char str[maxn];
ull Hash[maxn],base[maxn],p=133331,A[maxn],B[maxn];
ll dp[maxn];
int main(){
base[1]=1;for(int i=2;i<maxn;i++) base[i]=base[i-1]*p;
while(scanf("%s",str+1)!=-1){
int len=strlen(str+1);
A[0]=0;
for(int i=1;i<=len;i++) A[i]=A[i-1]*p+str[i]-'a'+1;
B[0]=0;
for(int i=1;i<=len;i++) B[i]=B[i-1]+(str[i]-'a'+1)*base[i];
ll ans=0;
for(int i=1;i<=len;i++){
if(A[i]==B[i]){
dp[i]=dp[i>>1]+1;
ans+=dp[i];
}
}
printf("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- (Coderforces 873C)C. Strange Game On Matrix 贪心+模拟
- Coderforces 354Div2
- coderforces 706B Interesting drink(二分)
- Coderforces 730H 730G
- coderforce Educational Codeforces Round 6 E.New Year Tree
- coderforces--418div2 B. An express train to reveries
- Coderforces cAPS lOCK
- Coderforces Insomnia cure
- coderforces 138CMushroom Gnomes - 2线段树
- (Coderforces 875A)A. Classroom Watch 暴力 + 思维
- Coderforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
- CoderForces #282 Dic2 D--Obsessive String
- coderforces--418 div2 C. An impassioned circulation of affection
- coderforces Stones on the Table
- Coderforces Nearly Lucky Number
- Codeforces Gym 101142 C. CodeCoder vs TopForces
- 计蒜客 CodeCoder vs TopForces
- Codeforces Coder-Strike 2014 (Div. 2) C: Online Meeting 来得最早走得最晚的leader
- coderforces 690D1 - The Wall (easy)(DFS)
- Coderforces Word Capitalization