洛谷P2463 Sandy的卡片【后綴數組】【二分】

題目描述

Sandy和Sue的熱衷于收集干脆面中的卡片。

然而,Sue收集卡片是因為卡片上漂亮的人物形象,而Sandy則是為了積攢卡片兌換超炫的人物模型。

每一張卡片都由一些數字進行標記,第i張卡片的序列長度為Mi,要想兌換人物模型,首先必須要集夠N張卡片,對于這N張卡片,如果他們都有一個相同的子串長度為k,則可以兌換一個等級為k的人物模型。相同的定義為:兩個子串長度相同且一個串的全部元素加上一個數就會變成另一個串。

Sandy的卡片數遠遠小于要求的N,于是Sue決定在Sandy的生日將自己的卡片送給Sandy,在Sue的幫助下,Sandy終于集夠了N張卡片,但是,Sandy并不清楚他可以兌換到哪個等級的人物模型,現在,請你幫助Sandy和Sue,看看他們最高能夠得到哪個等級的人物模型。

輸入輸出格式

輸入格式:

?

第一行為一個數N,表示可以兌換人物模型最少需要的卡片數,即Sandy現在有的卡片數

第i+1行到第i+N行每行第一個數為第i張卡片序列的長度Mi,之后j+1到j+1+Mi個數,用空格分隔,分別表示序列中的第j個數

?

輸出格式:

?

一個數k,表示可以獲得的最高等級。

?

輸入輸出樣例

輸入樣例#1:?復制
2
2 1 2
3 4 5 9
輸出樣例#1:?復制
2

說明

數據范圍:

30%的數據保證n<=50

100%的數據保證n<=1000,M<=1000,2<=Mi<=101

?

題意:

給定幾串序列,問他們最長公共子序列是多長。

思路:

類似之前做的Muisical, Themehttps://www.cnblogs.com/wyboooo/p/9865919.html

都是可以通過增加一個定值,所以只需要處理間隔。

不同的地方在于這個題目是多個不同的串。可以想到的是,是不是可以將這些串拼接起來。

拼起來就需要考慮一個問題,找到的這個串應該要避免他們跨越了拼接的那個間隔。

所以首先我們應該要用一個原來的串中從來沒有出現過的字符來分隔兩個串。

其次,當我們二分尋找答案的時候,應該要考慮我們找到的這個區間,他們表示的后綴的首字母也就是sa[i],是不是分別屬于n個串。

所以我們要標好每個位置屬于哪個串。

  1 #include <iostream>
  2 #include <set>
  3 #include <cmath>
  4 #include <stdio.h>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <map>
 10 using namespace std;
 11 typedef long long LL;
 12 #define inf 0x7f7f7f7f
 13 
 14 const int maxn = 1005;
 15 int a[maxn][maxn], s[maxn * maxn], s_len[maxn], id[maxn * maxn], vis[maxn];
 16 int sa[maxn * maxn];
 17 int t1[maxn * maxn], t2[maxn * maxn], c[maxn * maxn];
 18 int rnk[maxn * maxn], height[maxn * maxn];
 19 int n, len;
 20 
 21 void build_sa(int s[], int n, int m)
 22 {
 23     int i, j, p, *x = t1, *y = t2;
 24     for(i = 0; i < m; i++)c[i] = 0;
 25     for(i = 0; i < n; i++)c[x[i] = s[i]]++;
 26     for(i = 1; i < m; i++)c[i] += c[i - 1];
 27     for(i = n - 1; i >= 0; i--)sa[--c[x[i]]] = i;
 28     for(j = 1; j <= n; j <<= 1){
 29         p = 0;
 30         for(i = n - j; i < n; i++)y[p++] = i;
 31         for(i = 0; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j;
 32         for(i = 0; i < m; i++)c[i] = 0;
 33         for(i = 0; i < n; i++)c[x[y[i]]]++;
 34         for(i = 1; i < m; i++)c[i] += c[i - 1];
 35         for(i = n - 1; i >= 0; i--)sa[--c[x[y[i]]]] = y[i];
 36         swap(x, y);
 37         p = 1;
 38         x[sa[0]] = 0;
 39         for(i = 1; i < n; i++){
 40             x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1 : p++;
 41         }
 42         if(p >= n)break;
 43         m = p;
 44     }
 45 }
 46 
 47 void get_height(int s[], int n)
 48 {
 49     int i, j, k = 0;
 50     for(i = 0; i <= n; i++)rnk[sa[i]] = i;
 51     for(i = 1; i <= n; i++){
 52         if(k)k--;
 53         j = sa[rnk[i] - 1];
 54         while(s[i + k] == s[j + k])k++;
 55         height[rnk[i]] = k;
 56     }
 57 }
 58 int sta[maxn * maxn], top;
 59 bool check(int mid)
 60 {
 61     while(top)vis[sta[top--]] = false;
 62     for(int i = 1; i <= len; i++){
 63         if(height[i] < mid){
 64             while(top)vis[sta[top--]] = false;
 65         }
 66         if(!vis[id[sa[i]]]){
 67             vis[id[sa[i]]] = true;
 68             sta[++top] = id[sa[i]];
 69             if(top == n)return true;
 70         }
 71     }
 72     return false;
 73 }
 74 
 75 int main()
 76 {
 77     scanf("%d", &n);
 78     len = 0;
 79     int mmx = -1;
 80     for(int i = 1; i <= n; i++){
 81         vis[i] = false;
 82         scanf("%d", &s_len[i]);
 83         for(int j = 1; j <= s_len[i]; j++){
 84             scanf("%d", &a[i][j]);
 85             if(j)mmx = max(mmx, a[i][j] - a[i][j - 1]);
 86         }
 87     }
 88     int mmin = inf;
 89     for(int i = 1; i <= n; i++){
 90         for(int j = 2; j <= s_len[i]; j++){
 91             s[++len] = a[i][j] - a[i][j - 1];
 92             id[len] = i;
 93             mmin = min(mmin, s[len]);
 94         }
 95         s[++len] = ++mmx;
 96     }
 97     for(int i = 1; i <= len; i++){
 98         s[i] = s[i] - mmin + 1;
 99         //cout<<s[i]<<endl;
100     }
101 
102     build_sa(s, len + 1, 2000);
103     /*for(int i = 1; i <= len; i++){
104         cout<<sa[i]<<endl;
105     }*/
106     get_height(s, len);
107     /*cout<<len<<endl;
108     for(int i = 2; i <= len; i++){
109         cout<<height[i]<<endl;
110     }*/
111     //cout<<len<<endl;
112     int st = 0, ed = len, ans = -1;
113     while(st <= ed){
114         int mid = (st + ed) / 2;
115         if(check(mid)){
116             st = mid + 1;
117             ans = mid;
118         }
119         else{
120             ed = mid - 1;
121         }
122     }
123 
124     printf("%d\n", ans + 1);
125     return 0;
126 }

?

轉載于:https://www.cnblogs.com/wyboooo/p/9876295.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279076.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279076.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279076.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php獲取一個文件名的函數,PHP 文件系統函數之獲取文件名及文件名后綴-php文件...

獲取文件名(包含擴展):1.用PHP 文件函數 basename獲取例&#xff1a;$filename "/home/httpd/html/index.php";$file basename($filename);2.先獲取位置再獲取文件名例:$filename "/home/httpd/html/index.php";$pos strrpos($filename, /);if ($pos …

tasker使用手冊_如何開始使用Tasker調整Android手機

tasker使用手冊Tasker is a powerful app for Android that lets you customize how your phone works and automate tasks. Unfortunately, it’s got a bit of a learning curve. We’re here to show you how to get started and turn your phone into a flashlight in the …

iPhone 軟件:xlate free 編碼的好幫手!

功能菜單&#xff1a; 1 文本 2 二進制 3 Char 值 4 Base64 5 反向 如果需要把一段中文編碼請選擇UTF16&#xff0c;如果是英文就選擇UTF8。對于需要經常使用編碼切換的朋友是個好幫手。 也可以用來簡單加密&#xff1a;我們先在文本狀態下輸入一段不想讓別人知道或需要保密的文…

linkbox php,win10 docker-toolsbox 搭建php開發環境的教程

下載鏡像docker pull mysql:5.7docker pull php:7.2-fpmdocker pull nginxdocker pull redis:3.2設置共享文件宿主機創建目錄E:\wnmp\mysql57\confE:\wnmp\mysql57\logE:\wnmp\php72\confE:\wnmp\php72\confE:\wnmp\nginx\confE:\wnmp\nginx\confE:\wnmp\wwwvmware設置文件共享…

sublime text3:提示 There are no packages available installation 解決方案

純屬記錄&#xff0c;下次能找到解決。 第一步&#xff1a; 在sublime Text3界面按 ctrl 出現一個輸入框界面 第二步&#xff1a;在輸入框輸入&#xff1a; import urllib.request,os,hashlib; h eb2297e1a458f27d836c04bb0cbaf282 d0e7a3098092775ccb37ca9d6b2e4b7d; pf Pa…

如何提取幻燈片表格_如何查看對Google文檔,表格或幻燈片文件的最新更改

如何提取幻燈片表格The Google Suite offers you a handy way to view all the changes that have occurred in a file on Google Docs, Sheets, or Slides. This is extremely useful when you’ve made lots of changes to a file or are working as part of a team and need…

[20171130]關于rman的一些總結.txt

[20171130]關于rman的一些總結.txt --//最近一直做rman相關測試,測試那個亂,沒辦法.無法從周圍的人獲得幫助,純粹是自己的亂猜,亂測,不知道別人是否能看懂我寫的東西. --//有必要做一些總結,不一定對,僅僅是我當前的看法. 1.數據文件備份集中,文件頭是最后寫到備份集文件的. 2.…

支付寶紅包php,支付寶紅包賞金跳轉源碼,一鍵復制紅包碼,裂變推廣

[html]代碼庫支付寶到店紅包搜索碼跳轉推廣裂變-引流*{padding:0;margin:0;}.main{overflow: hidden;}a {color:black;}.main img{width:100%;outline-width:0px;vertical-align:top;}.main{position: relative;}.main .copy-container{width: 100%;height: 0.42rem;position: …

apt-get更新軟件包_如何使用Apt-fast加速軟件包下載和更新

apt-get更新軟件包By Default, Ubuntu uses apt-get to install packages and updates. Apt-get is a good tool but you can get much faster download speeds using Apt-Fast when downloading and updating your Ubuntu box. 默認情況下&#xff0c;Ubuntu使用apt-get安裝軟…

FallbackFactory啟動的時候拋出異常

在Hystrix做熔斷的時候&#xff0c;開始用的是FallBack&#xff0c;后來為了找出為啥exception&#xff0c;然后就用了FallBackFactory。但是奇怪的是&#xff0c;一起動就拋出異常&#xff0c;真的是百思不得騎姐&#xff0c;錯了其解。后來在github上找到了解答&#xff1a;h…

制作首頁的顯示列表

1. 在首頁添加顯示問答的列表&#xff0c;并定義好相應的樣式。 無序列表 <ul > <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> {% block body %}<div class"container"><div class"row clearfi…

php xxtea加密,php - esp32和php XXTEA字符串加密 - SO中文參考 - www.soinside.com

輸入具有不同的數據類型可能會導致此問題&#xff0c;因為當前沒有任何類型或范圍檢查的XXTEA實現。或者它可能是由于所涉及的兩臺計算機的不同端序行為&#xff0c;因為二進制文件通常存儲為由字節構造的字數組。或者可能是由于缺少正式加密特定字符串和密鑰的官方或標準參考示…

ipad iphone開發_如何在iPad或iPhone上使用外部GPS設備

ipad iphone開發If you bought a Wi-Fi only iPad and now you wish you could use GPS with it, this is the guide for you. Follow along to hook your iPad up to an external GPS unit and/or GPS-enabled smartphone phone. 如果您購買了僅支持Wi-Fi的iPad&#xff0c;現…

jQuery系列(十四):jQuery中的ajax

1、什么是ajax AJAX 異步的javascript和XML&#xff08;Asynchronous Javascript and XML&#xff09; 簡言之&#xff0c;在不重載整個網頁的情況下&#xff0c;AJAX通過后臺加載數據&#xff0c;并在網頁上進行顯示。 通過 jQuery AJAX 方法&#xff0c;您能夠使用 HTTP Get…

flex 布局以及樣式

1.Flex是Flexible Box的縮寫&#xff0c;意為”彈性布局”&#xff0c;用來為盒狀模型提供最大的靈活性2.任何一個容器都可以用flex布局&#xff08;注意&#xff0c;設為Flex布局以后&#xff0c;子元素的float、clear和vertical-align屬性將失效&#xff09; 采用Flex布局的元…

java swing列表數據加監聽,【Java Swing公開課|Java監聽列表項選擇事件怎么用,看完這篇文章你一定就會了】- 環球網校...

【摘要】作為一門面向對象編程語言&#xff0c;Java吸收了C語言的優點&#xff0c;也展現了其強大的一面&#xff0c;我們能在各個地方看到其功能強大和簡單易用的兩個特征&#xff0c;當然&#xff0c;也吸引了很多程序員的注意力&#xff0c;所以就有人想了解Java的相關內容&…

fc-ae-1553_什么是AE-L,AF-L和*按鈕,它們的作用是什么?

fc-ae-1553DSLRs and mirrorless cameras have a lot of buttons. If you’re just starting to get the hang of manually controlling your camera, you’re probably wondering what all the—seemingly non-essential—ones do. Let’s take a look at the AE-L, AF-L, AF-…

PopsTabView--filter容器

PopsTabView是個filter容器,他可以自動,快速,構建不同篩選樣式,自由組合成一組tab. DownloadDownloadAuthorLicense篩選樣式篩選種類可自定義屬性單列單選,多選初始數據bean,篩選結果bean,tab樣式,篩選樣式多排單選,多選初始數據bean,篩選結果beantab樣式,篩選樣式雙列單項單選…

git 基本使用方法

git clone https://gitee.com/kuaiyiwazz.git //開始下載服務器項目文件&#xff08;后邊是服務地項目的地址&#xff09;git add . //這里有個點&#xff08;仔細看&#xff09;git status //檢查項目修改狀態git commit -m"注釋(修改的內容)" git push //添…

大學留級兩年不敢和家人說_您說什么:如何與家人保持聯系?

大學留級兩年不敢和家人說Earlier this week we asked you to share your tips, tricks, and techniques for staying connected when you’re away from your home broadband connection. Now we’re back with a roundup of what you said. 本周早些時候&#xff0c;我們要求…