利用KMP算法的預處理部分。
/*Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15324 Accepted: 6475 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n). Input Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case. Output For each s you should print the largest n such that s = a^n for some string a. Sample Input abcd aaaa ababab . Sample Output 1 4 3 Hint This problem has huge input, use scanf instead of cin to avoid time limit exceed.*/ #include <stdio.h> #include <string.h> #define MAX_STRING_LENGTH 1000005 int gaiNext[MAX_STRING_LENGTH]; char gacInputBuffer[MAX_STRING_LENGTH]; void KMP_CalcNext(char *vpcInputPattern) { int iPatternLen = 0; int iLoop = 0; int iNextIndex = -1; iPatternLen = strlen(vpcInputPattern); gaiNext[0] = -1; for (iLoop = 0; iLoop < iPatternLen; iLoop++) { while(0 <= iNextIndex && vpcInputPattern[iNextIndex] != vpcInputPattern[iLoop]) { iNextIndex = gaiNext[iNextIndex]; } if (0 > iNextIndex ||vpcInputPattern[iNextIndex] == vpcInputPattern[iLoop]) { iNextIndex++; gaiNext[iLoop+1] = iNextIndex; } } return; } // void KMP_CalcNext(char *vpcInputPattern) // { // int iPatternLen = 0; // int iLoop = 0; // int iNextIndex = -1; // // // iPatternLen = strlen(vpcInputPattern); // // gaiNext[0] = -1; // // while(iLoop < iPatternLen) // { // if(iNextIndex == -1||vpcInputPattern[iLoop] == vpcInputPattern[iNextIndex]) // { // iLoop++; // iNextIndex++; // gaiNext[iLoop] = iNextIndex; // }else // { // iNextIndex = gaiNext[iNextIndex]; // } // } // // return; // } int PowerStringsMain() { int iStrLen = 0; while (1) { scanf("%s",gacInputBuffer); if ('.' == gacInputBuffer[0]) { break; } iStrLen = strlen(gacInputBuffer); KMP_CalcNext(gacInputBuffer); if (0 != iStrLen%(iStrLen - gaiNext[iStrLen])) { printf("1/n"); } else { printf("%d/n",iStrLen/(iStrLen - gaiNext[iStrLen])); } } return 0; }
1 枚舉好用嗎? 數據字典型字段,枚舉比Integer好: 限定值,只能賦值枚舉的那幾個實例,不能像Integer隨便輸,保存和查詢的時候特別有用 含義明確,使用時不需要去查數據字典 顯示值跟存儲值直接映射,不需要手動轉換,比如1在頁面上
背景 今天在使用redis做分佈式緩存提供自增id的時候,發現了�� t Goods_key_37的亂碼問題;因爲存的是Long,然後我就自定義了一個RedisTemplate<string,long>的實例來使用 解決過程 1.剛開始說
本文分享自華爲雲社區《10分鐘搞懂各種內存溢出案例!!(含完整源碼,建議收藏)》,作者:冰 河。 作爲程序員,多多少少都會遇到一些內存溢出的場景,如果你還沒遇到,說明你工作的年限可能比較短,或者你根本就是個假程序員!哈哈,開個玩笑。今天,我
Ai technology 前言 AppBuilder上線了低代碼製作組件功能,可以通過工作流的方式構建自定義組件,完成簡單Agent無法完成的複雜功能,使得生成的文本更加定製化,
最近在刷一些問題的時候看到有下面一個問題 上面問的是當輸入的字符串爲什麼的時候返回 True 總結 在做題目的時候,第一次還做錯了。 這是因爲解答這個題目的時間只有 3 分鐘,沒有自己看題目 後來拿着程序跑了下。 p
簡介: 搶紅包是我們生活常用的社交功能, 這個功能最主要的特點就是用戶的併發請求高, 在系統設計上, 可以使用非常多的辦法來扛住用戶的高併發請求, 在本文中簡要介紹使用Redis緩存中間件來實現搶紅包算法, Redis是一個在內存中基
字符串是 Java 程序中最常用的數據結構之一。在 Java 中 String 類已經重載了"+",字符串可以直接使用"+"進行連接,也可以用StringBuilder/StringBuffer(StringBuilder是J2SE5 及以
Java 平臺提供了兩種類型的字符串操作方式:String 和 StringBuffer/StringBuilder,它們都可以儲存和操作字符串,區別如下: String 是隻讀字符串,也就意味着 String 引用的字符串內容是不能被改
1 概述 通道是Golang提供的一種基本類型,它可以實現在協程之間的單向通信和雙向通信、發送和接收數據、以及協程同步。 channel的本質是一個隊列,遵循先進先出原則。channel是線程安全的,在任何給定時間,一個數據被設計爲只有一
Java有8種基本數據類型,每種基本數據類型都提供了一種對應的包裝類型,從 Java 5 開始引入了自動裝箱/拆箱機制,使得二者可以相互轉換。 原始類型: boolean,char,byte,short,int,long,float,do
繼續運行 ?* 回憶上次內容 上次使用shell環境中的命令 命令 作用 c
前言 作爲表格產品的典型應用場景之一,幾乎所有的行業都會存在類 Excel 報表開發這樣的應用場景,而在這些應用場景中,經常會遇見下面的這些痛點: 報表數據往往來自多個不同的數據源,需要報表系統能夠同時連接多個數據源,並融合不同的數據格式
本文分享自華爲雲社區《GaussDB SQL基本語法示例-CASE表達式》,作者:Gauss松鼠會小助手2。 一、前言 SQL是用於訪問和處理數據庫的標準計算機語言。GaussDB支持SQL標準(默認支持SQL2、SQL3和SQL4的主要
開源項目推薦 xlskubectl 用於控制 Kubernetes 集羣的電子表格。xlskubectl 將 Google Spreadsheet 與 Kubernetes 集成。你可以通過用於跟蹤費用的同一電子表格來管理集羣。 git-
導讀 本文介紹了 TiDB 中 MVCC(多版本併發控制)機制的原理和相關排查手段。 TiDB 使用 MVCC 機制實現事務,在寫入新數據時不會直接替換舊數據,而是保留舊數據的同時以時間戳區分版本。 當歷史版本堆積過多時,會導致讀寫性能下降