python爬取企名科技創業專案資訊
阿新 • • 發佈:2022-05-31
列舉迴文串的中心點 ,二分左右的長度
判斷兩個字串時用hash
#include<iostream> #include <algorithm> #include <cstring> using namespace std; #define ll unsigned long long const int N=1e6+5; char a[N]; ll h1[N],h2[N],pow[N]; ll bas=131; ll f1(int l,int r){ return h1[r]-h1[l-1]*pow[r-l+1]; } ll f2(int l,int r){ return h2[l]-h2[r+1]*pow[r-l+1]; } void sov(int cas){ int i,j,l,r; h1[0]=h2[0]=0; int ans=0; int n=strlen(a+1); h2[n+1]=0; for(i=1,j=n;i<=n;i++,j--){ h1[i]=h1[i-1]*bas+a[i]; h2[j]=h2[j+1]*bas+a[j]; } for(i=1;i<=n;i++){ l=0,r=min(i,n-i); int t=0; while(l<=r){ int md=(l+r)/2; if(f1(i-md,i-1)==f2(i+1,i+md)) l=md+1,t=md; else r=md-1; } ans=max(ans,t*2+1); } for(i=1;i<=n;i++){ l=0,r=min(i-1,n-i+1); int t=0; while(l<=r){ int md=(l+r)/2; if(f1(i-md,i-1)==f2(i,i+md-1)) l=md+1,t=md; else r=md-1; } ans=max(ans,t*2); } printf("Case %d: %d\n",cas,ans); } main(){ int i; pow[0]=1; for(i=1;i<=1e6;i++) pow[i]=pow[i-1]*bas; int cas=0; while(cin>>a+1,a[1]!='E'){ sov(++cas); } }