日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3974-Palindrome

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3974-Palindrome 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Description

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, “Can you propose an efficient algorithm to find the length of the largest palindrome in a string?”

A string is said to be a palindrome if it reads the same both forwards and backwards, for example “madam” is a palindrome while “acm” is not.

The students recognized that this is a classical problem but couldn’t come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said “Okay, I’ve a better algorithm” and before he starts to explain his idea he stopped for a moment and then said “Well, I’ve an even better algorithm!”.

If you think you know Andy’s final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string “END” (quotes for clarity).
Output

For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input

abcbabcbabcba
abacacbaaaab
END
Sample Output

Case 1: 13
Case 2: 6
Source

Seventh ACM Egyptian National Programming Contest
.
.
.
.
.
.

分析

最長回文子串,也是子串+長度類型的問題,此處使用字符串哈希+二分解決。
對字符串進行正序倒序兩次哈希,之后二分長度,對每個長度len枚舉子串的哈希值,判斷相同位置的子串正逆序哈希值是否相同,相同則代表為回文串。
復雜度O(n*log(n))。
.
.
.
.
.

程序:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; const unsigned long long zd=30007uLL; unsigned long long h[1000001],p[1000001],xp[1000001]; char s[1000001]; int n;unsigned long long askhash(int l,int r) {if (l==0) return h[r];return h[r]-h[l-1]*xp[r-l+1]; }unsigned long long askp(int l,int r) {if (r==n-1) return p[l];return p[l]-p[r+1]*xp[r-l+1]; }void inithash() {h[0]=s[0];for(int i=1;i<n;i++)h[i]=h[i-1]*zd+s[i];p[n-1]=s[n-1];for(int i=n-2;i>=0;i--)p[i]=p[i+1]*zd+s[i]; }bool check(int len) {unsigned long long ht,pt;for (int i=0,l,r;i+len-1<n;i++){l=i;r=i+len-1;ht=askhash(l,r);pt=askp(l,r);if (ht==pt) return true;}return false; }int main() {xp[0]=1;for (int i=1;i<1000001;i++)xp[i]=xp[i-1]*zd;int tj=0;while (1){cin>>s;if (s[0]=='E') break;n=strlen(s);for (int i=0;i<n;i++)s[i]=s[i]-'a'+1;inithash();vector<int> a,b;for(int i=1;i<=n;i++)if (i % 2) a.push_back(i); else b.push_back(i);int ans=0,l=0,r=a.size()-1,m;while (l<=r){m=(l+r)>>1;if (check(a[m])){l=m+1;ans=a[m];} else r=m-1;}l=0;r=b.size()-1;while (l<=r){m=(l+r)>>1;if (check(b[m])){l=m+1;ans=max(ans,b[m]);} else r=m-1;}tj++;cout<<"Case "<<tj<<':'<<' '<<ans<<endl;}return 0; }

轉載于:https://www.cnblogs.com/YYC-0304/p/9499892.html

總結

以上是生活随笔為你收集整理的POJ 3974-Palindrome的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。