#jl-05 字串題解
原題:字串
描述現在有乙個長度為 n 的字串,它的字典集為 m .也就是說每一位有 m 種不同的字母可以選。對於這個字串所有連續的長度為 k 的子串都必 須是回文串,請問有多少種不同的方案。
由於答案可能很大,請將答案 mod 1e9 + 7 .
輸入一共一行,三個整數表示 n,m,k .
n,m,k ≤ 2000
輸出一共一行,乙個整數表示最後答案。
輸入樣例 1
5 2 4
輸出樣例 1
思路題解:
回文串的實現
回文串長度為k
外層for迴圈i從1遍歷到n
內層while迴圈union i—k,i+1—k-1,i+2—k-2…
資料結構——並查集(一開始不知道並查集,想了好久,lgjnb)
find:確定元素屬於哪乙個子集。這個確定方法就是不斷向上查詢找到它的根節點,它可以被用來確定兩個元素是否屬於同一子集。
union:將兩個子集合並成同乙個集合。
init:並查集初始化。(***的題解初始化在主函式中,這樣其實不太好,想了想下次應該單獨寫出來,這樣主函式的邏輯會更加清晰,也方便呼叫)
核心**:
int
find
(int k)
void
union
(int x1,
int x2)
void
init()
ac**
#include
#include
#include
#include
#include
#include
#include
using
namespace std;
const
int modnum=
1000000007
;int f[
10000
],n,m;
intfind
(int k)
void
union
(int x1,
int x2)
//令x1的根節點為x2的根節點
intmain()
if(k==
1||k==0)
cout
}else
for(
int i=
1;i<=n;i++)}
for(
int i=
1;i<=n;i++
) ans=1;
for(
int i=
1;i<=sum;i++
) cout<}return0;
}
PHP學習筆記(05) 字串
1.字串的定義方式 2.單雙引號的對比,轉義,解析,速度 3.常用函式 header content type text html charset utf 8 heredoc nowdoc定義文字 str3 str3,str4 intro 遠看山無色,近聽水無聲 intro echo str4 no...
day05字串和序列
字串與序列 1.字串 若需要在字串內容 現引號 或 可以使用轉義符號 對字串中的符號進行轉義 2.相關函式 list sub 把乙個可迭代物件轉換為列表 tuple sub 把乙個可迭代物件轉換為元組 str obj 把obj物件轉換為字串 len sub 返回sub包含元素的個數 max sub ...
LeetCode題解 394 字串解碼
由於括號內巢狀括號,與棧的先進後出一致。class solution object def decodestring self,s type s str rtype str stack,multi,res 0,stack用於實現儲存括號的巢狀關係 multi用於儲存括號前的係數 res用於返回結果 ...