kmp入門小結

void get_next(char *s)
{int len = strlen(s);int j = 0; int k = -1;while (j < len){if (k == -1 || s[j] == s[k]){j++; k++; next[j] = k;}else k = next[k];}
}

設t = next[i]; next[i] 表示的是 i之前最大的t滿足 s[0...t-1] ?= ?s[i-t...i-1]

比如 0123 4 0123 5 ,next[9] = 4.

結論:len - next[len] 為最小覆蓋子串。

Poj3461?Oulipo

題意:給出兩個串,問第二個串在第一個串中出現的次數

/* ***********************************************Author        : 一個西瓜Mail          : 879447570@qq.comCreated Time  : 2015-04-07 16:24:21Problem       : Oulipo
************************************************ */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
//typedef __int64 LL; const int maxn = 11111;
int next[maxn];
char str[maxn];
char str1[maxn*100];void get_next(char *s)
{next[0] = -1;int j = 0 ;int k = -1;int len = strlen(s);while(j<len){if(k==-1||s[j]==s[k]){j++;k++;next[j] = k;}else k = next[k];}
}int gao(char *s1,char *s2)
{int ans = 0;int len1 = strlen(s1);int len2 =strlen(s2);int j = -1;int k = -1;while(j<len2){if(k==-1||s1[k]==s2[j]){j++;k++;//printf("%d %d\n",j,k);
        }else k = next[k];if(k==len1){ans++;k = next[k];}}return ans;
}int main()
{int T;cin>>T;while(T--){cin>>str;cin>>str1;get_next(str);int k = gao(str,str1);cout<<k<<endl;}return 0;
}
View Code

Poj1961?Period

就用到上面那個結論了

/* ***********************************************Author        : 一個西瓜Mail          : 879447570@qq.comCreated Time  : 2015-04-07 17:50:15Problem       : Period
************************************************ */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
//typedef __int64 LL; const int maxn = 1111111;
int next[maxn];
char str[maxn];
void get_next(char *s)
{int len =strlen(s);int j = 0;int k = -1;next[0] = -1;while(j<len){if(k==-1||s[j]==s[k]){j++;k++; next[j] = k;}else k = next[k];}
}int main()
{int Icase = 0 ;int n;while(scanf("%d",&n)&&n){if(Icase) cout<<endl;printf("Test case #%d\n",++Icase);scanf("%s",str);get_next(str);int len = strlen(str); for(int i = 1;i<len;i++){int t = i+1;if(t%(t - next[t])==0&&(t/(t-next[t])!=1)){printf("%d %d\n",t,t/(t - next[t]));}}}return 0;
}
View Code

Poj2752?Seek the Name, Seek the Fame

給出一個串,問哪些既是前綴,又是后綴。

next數組不就是,到當前串的第i個位置,既是這個子串的前綴又是后綴的最長前綴么。迭代下去求就行了。

/* ***********************************************Author        : 一個西瓜Mail          : 879447570@qq.comCreated Time  : 2015-04-07 17:28:12Problem       : Seek the Name, Seek the Fame
************************************************ */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
//typedef __int64 LL; const int maxn = 4*1e5 +10;
int next[maxn];
char str[maxn];void get_next(char *s)
{next[0] = -1;int len =strlen(s);int j = 0 ;int k = -1;while(j<len){if(k==-1||s[j]==s[k]){j++;k++;next[j] =  k;}else k = next[k];}
}
vector<int> q;
int main()
{while(scanf("%s",str)!=EOF){get_next(str);q.clear();int k = strlen(str);q.push_back(k);k = next[k];while(k){q.push_back(k);k = next[k];}for(int i = q.size() - 1;i>=0;i--){printf("%d ",q[i]);}cout<<endl;}return 0;
}
View Code

Poj2406?Power Strings

和1961一樣的。

/* ***********************************************Author        : 一個西瓜Mail          : 879447570@qq.comCreated Time  : 2015-04-07 17:50:40Problem       : Power Strings
************************************************ */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
//typedef __int64 LL; const int maxn = 1e6+10;
char str[maxn];
int next[maxn];void get_next(char *s)
{int len =strlen(s);int j = 0 ;int k = -1;next[0] = -1;while(j<len){if(k==-1||s[j]==s[k]){j++;k++; next[j] = k;}else k = next[k];}
}int main() {while(cin>>str){if(str[0]=='.') break;get_next(str);int len = strlen(str);if(len%(len-next[len])==0)cout<<len / (len - next[len])<<endl;else cout<<1<<endl;}return 0; 
}
View Code

?

轉載于:https://www.cnblogs.com/yigexigua/p/4442651.html

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

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

相關文章

基于visual Studio2013解決面試題之0807strstr函數

&#xfeff;&#xfeff;&#xfeff;題目解決代碼及點評/*寫strstr函數簡單的遍歷去查找吧 */#include <iostream> #include <stdio.h>const char *my_strstr(const char *str, const char *sub_str) {// 遍歷for(int i 0; str[i] ! \0; i){int tem i; //tem保…

aop在項目中的實際運用_mypy在實際項目中的應用

我認為靜態類型似乎被吹捧過高了。盡管如此&#xff0c;mypy極低的侵入性能帶來許多好處。關于如何在現有的Python項目中添加類型&#xff0c;以下是我的一些想法&#xff0c;大致按重要性排序。首先確保mypy成功運行 Mypy上手時兩個很常見的問題有&#xff1a;1.Mypy沒有作為構…

face alignment by 3000 fps系列學習總結(三)

訓練 我們主要以3000fps matlab實現為敘述主體。 總體目標 我們需要為68個特征點的每一個特征點訓練5棵隨機樹&#xff0c;每棵樹4層深&#xff0c;即為所謂的隨機森林。 開始訓練 分配樣本 事實上&#xff0c;對于每個特征點&#xff0c;要訓練隨機森林&#xff0c;我們需…

HDU 2049 不容易系列之(4)——考新郎( 錯排 )

鏈接&#xff1a;傳送門思路&#xff1a;錯排水題&#xff0c;從N個人中選出M個人進行錯排&#xff0c;即 C(n,m)*d[m]補充&#xff1a;組合數C(n,m)能用double計算嗎&#xff1f;第二部分有解釋 Part 1. 分別求出來組合數的分子和分母然后相除/******************************…

級聯sql

select ID, PID, NAME,KEY from HS_DICT start with KEY HS_EXP_WORK_LOCATIONconnect by prior ID PID order by DISPLAYORDER轉載于:https://www.cnblogs.com/dazhaxie/p/3483532.html

獲取母版中的控件

1 通過findcontrol找控件ID需要在此事件中~因為Page_load中時是先內容頁加載然后才是母版頁加載 protected void Page_LoadComplete(object sender, EventArgs e) { Label2.Text "現在時間是" (Master.FindControl("Label1") as Label).Text; if (Reques…

linux服務器選ubantu或centos_如何通過SSH連接阿里云上的Linux系統

首先SSH是啥&#xff0c;維基一下&#xff1a;Secure Shell&#xff08;安全外殼協議&#xff0c;簡稱SSH&#xff09;是一種加密的網絡傳輸協議&#xff0c;可在不安全的網絡中為網絡服務提供安全的傳輸環境[1]。SSH通過在網絡中創建安全隧道來實現SSH客戶端與服務器之間的連接…

face alignment by 3000 fps系列學習總結

我們主要講一講Github上給出的matlab開源代碼《jwyang/face-alignment》的配置。 首先聲明&#xff1a;本人第一次配置的時候也是參考了csdn一個作者和github給出的說明配置成功的。其實后來想想很簡單的&#xff0c;但是可能對于初學者&#xff0c;還是有一定的困難。為此&am…

paypal之nodejs 框架 Kraken-js 源碼分析

本文是基于 kraken-js 0.6.1 版本的 關于如何使用kraken-js 可以去看看官網的使用文檔 點擊這里 。kraken-js 是基于express之上的&#xff0c;目的在于讓工程師更多的去關注代碼邏輯&#xff0c;少關注自身的開發環境&#xff0c;所以他將express所有的一些公用的配置都寫在了…

go build 參數_Go語言 通過go bulid -tags 實現編譯控制

Go語言提供的build tag 條件編譯特性&#xff0c;顧名思義&#xff0c;只有在特定條件下才會構建對應的代碼。比如下面的源文件只有在設置debug構建標志時才會被構建&#xff1a;// build debugpackage mainvar buildMode "debug"可以用以下命令構建&#xff1a;go …

selinux 的管理

第十單元selinux 的管理一 顯示及更改 SELINUX 模式getenforce ###顯示selinux模式setenforce 0|1 ##0指permissive警告&#xff0c;1 表示 enforcing強制###vim /etc/sysconfig/selinux ###修改selinux開機狀態###注&#xff1a;disable表示關閉&…

ubuntu15.10下安裝opencv2.4.9python上調用opencv庫

對于centos&#xff0c;可以參考&#xff1a;Install OpenCV-Python in Fedora 如果IPP難以下載可以在cmake時禁掉它&#xff0c;只需&#xff1a;cmake -DWITH_IPPOFF OpenCV3.3CUDA9.0 安裝過程中遇到的問題&#xff0c;解析&#xff1a; https://blog.csdn.net/u014613745/a…

【轉】jquery 注冊事件的方法

原文鏈接&#xff1a;http://outofmemory.cn/code-snippet/2123/jquery-zhuce-event-method 1.使用事件名來綁定&#xff0c;可用的事件名有 change,click,dblclick,error,focus,focusin,focusout,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,…

ffmpeg 時間戳

轉http://blog.csdn.net/yfh1985sdq/article/details/5721953 AVpacket里的時間戳pts和dts.單位好像是us. 問 : 時間戳pts和dts,這里兩個時間戳各有什么意義? 答 : 顯示時間,解碼時間. DTS&#xff1a;decoding time stamp PTS&#xff1a;presentation time stamp Generally …

鍵盤改鍵軟件_一秒五鍵,一鍵三招,萬種光污染,杜伽K310櫻桃軸機械鍵盤感受...

機械鍵盤我一直用的青軸&#xff0c;或者各種其他名字但其實本質就是青軸的。喜歡青軸那種清脆的聲音&#xff0c;在我聽來如同山間小溪流水般的叮咚。不過這聲音在夜間分外的具有穿透力&#xff0c;更會在人身體不好的時候難以承受&#xff0c;所以每每用過之后卻又不得不換回…

ubuntu 15.10下cmake 的安裝

因為原先ubuntu自帶的cmake有點舊&#xff0c;就想著安裝個最新的&#xff0c;可是直接安裝卡在了某一步上&#xff0c;后面有說明。現將正確的安裝方法列出來。1.卸載原有的版本sudo apt-get autoremove cmake2. 下載最新的cmake :https://cmake.org/download/3. 解壓&#xf…

codeigniter鉤子的使用

CodeIgniter 的鉤子功能&#xff0c;使得我們可以在不修改系統核心文件的基礎上&#xff0c;來改變或增加系統的核心運行功能。可是鉤子究竟該怎么用呢&#xff1f;雖然不是很難&#xff0c;不過很多剛用ci的朋友可能還是不明白怎么用。 通過本文的簡單實例&#xff0c;大家一下…

wxWidgets之wxGrid控件

1. 介紹wxGrid控件時wxWidgets界面庫中內置的網格控件。通經常使用來顯示表格數據。該控件擁有強大的功能。開發人員可依據自己的需求對其進行定制。 2. 經常使用API 構造函數&#xff1a;wxGrid ()wxGrid (wxWindow *parent, wxWindowID id, const wxPoint &poswxDef…

powerdesigner畫關系圖_想畫好手繪,這些圖你一定要畫一下!

畫好手繪除了對透視要深入了解掌握以及線條運用把握之外&#xff0c;還有很重要的就是要對空間物體的關系、比例、光影關系都要理解透徹。大體快可分割成多個x小體塊。其實當年學習的繪畫基礎也是畫好手繪的基礎&#xff0c;畫手繪依然需要去理解整體畫面的空間黑白灰、物體穿插…

C#,pdf文件轉換成圖片文件。

本文采用Adobe Acrobat9.0的COM組件&#xff0c;將Pdf文件的每一頁轉換成對應的圖片文件。 開發環境&#xff1a;VS2010&#xff0c;.Net Framework4.0&#xff0c;Adobe Acrobat9.0。 工程中添加COM引用&#xff1a;Adobe Acrobat 9.0 Type Library&#xff08;必須裝了Adobe …