• [NOI.AC#34]palinedrome 字符串hash+贪心


    容易看出,只要从两边往中间扫描,碰到相等的就直接分割然后加入答案即可,判断相等用字符串hash

    #include<bits/stdc++.h>
    #define REP(i,a,b) for(int i(a);i<=(b);++i)
    #define dbg(...) fprintf(stderr,__VA_ARGS__)
    using namespace std;
    typedef long long ll;
    typedef unsigned int uint;
    typedef unsigned long long ull;
    template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;}
    template<typename T,typename U>inline char smax(T&x,const U&y){return x<y?x=y,1:0;}
    const int N=1e6+5;
    int n;
    ull cf[N],hl,hr;
    char s[N];
    int main(){
    	int t;scanf("%d",&t);n=1e5;cf[0]=1;
    	REP(i,1,n)cf[i]=cf[i-1]*131;
    	while(t--){
    		scanf("%s",s+1),n=strlen(s+1);
    		int ans=0;hl=hr=0;
    		for(int l=1,r=n,p=n;;++l,--r){
    			if(l>=r){
    				if(l==r||hl!=hr)++ans;
    				break;
    			}
    			hl=hl*131+s[l],hr+=cf[p-r]*s[r];
    			if(hl==hr)ans+=2,hl=hr=0,p=r-1;
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    
    
    
  • 相关阅读:
    深入理解多态..............................
    走过路过 不要错过..
    进军C#..
    员工打卡....
    MySQL
    MySQL
    MySQL
    MySQL
    MySQL
    MySQL
  • 原文地址:https://www.cnblogs.com/HolyK/p/9839748.html
Copyright © 2020-2023  润新知