HDU 5318 The Goddess Of The Moon(递推+矩阵优化)

题目链接:传送门

题意:

相当于有一个长度为m的路,我们有n种砖,每种砖被表示为一个字符串,一个长度大于等于2的后缀等于

另一个字符串的前缀那么那一块砖就可以放在这块砖的后面。

分析:

这个就是常见的铺砖的那个模型变化而来的,但是这题的递推关系需要根据题目给定的字符串的结构来决

定,由于m比较大,我们需要用矩阵来优化,根据题目给定的字符串来确定状态转移矩阵A,初始的矩阵为单

位矩阵I,然后ans = A^(m-1)*I.在确定A的时候注意给字符串去重。

代码如下:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <set>
using namespace std;

const int maxn = 55;

typedef long long LL;

const LL mod = 1e9+7;

int n,m,cnt;

string str[maxn];

set<string > st;

struct matrix{
    LL a[maxn][maxn];
    matrix(){
        memset(a,0,sizeof(a));
    }
};

matrix I,A;

void init(){
    for(int i=0;i<maxn;i++)
        for(int j=0;j<maxn;j++)
            I.a[i][j]=(i==j);
}

int judge(string a,string b){
    for(int j=2;j<=a.length()&&j<=b.length();j++){
        bool tag = 0;
        for(int i=0;i<j;i++){
            if(a[a.length()-j+i]!=b[i]){
                tag=1;
                break;
            }
        }
        if(!tag) return 1;
    }
    return 0;
}

void getA(){
    for(int i=0;i<cnt;i++){
        for(int j=0;j<cnt;j++){
            A.a[i][j]=judge(str[i],str[j]);
        }
    }
}

matrix multi(matrix A,matrix B){
    matrix C;
    for(int i=0;i<cnt;i++){
        for(int j=0;j<cnt;j++){
            for(int k=0;k<cnt;k++){
                C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j]%mod)%mod;
            }
        }
    }
    return C;
}

LL quick(matrix A,int b){
    matrix ans=I;
    while(b){
        if(b&1) ans=multi(ans,A);
        b>>=1;
        A=multi(A,A);
    }
    LL sum=0;
    for(int i=0;i<cnt;i++){
        for(int j=0;j<cnt;j++){
            sum=(sum+ans.a[i][j])%mod;
        }
    }
    return sum;
}

int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        if(n==0||m==0){
            puts("0");
            continue;
        }
        string s;
        st.clear();
        cnt=0;
        for(int i=0;i<n;i++){
            cin>>s;
            if(st.find(s)==st.end()){
                str[cnt++]=s;
                st.insert(s);
            }
        }
        getA();
        init();
        printf("%I64d\n",quick(A,m-1));
    }
	return 0;
}
/****
5
5 50
121 123 213 132 321
1 0 0 1 0
0 1 0 0 0
0 0 1 0 1
0 0 1 1 0
0 0 0 1 1
797922656
**/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-28 16:54:56

HDU 5318 The Goddess Of The Moon(递推+矩阵优化)的相关文章

hdu 5318 The Goddess Of The Moon(矩阵快速幂)

题目链接:hdu 5318 The Goddess Of The Moon 将50个串处理成50*50的矩阵,注意重复串. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 55; const int mod = 1e9+7; int N, M, A[maxn]; struct M

HDU 5318 The Goddess Of The Moon (矩阵快速幂)

题目链接:HDU 5318 The Goddess Of The Moon 题意:给出N串字符串,若是一个字符串的后缀与另一个字符串的前缀相同并且长度大于1,就表示这两个字符串是可以相连的,问M个字符串相连不同方案数为多少. 思路: 1.将输入的字符串预处理存入一个矩阵中,mp[i][j]=1说明str[i]与str[j]能相连,反之,则不能相连. 2.str[i]与str[j]能相连 转化为 i点到j点可达,那么就可以得到一个有向图,长度为M的意思就是 两点之间所走的步数为M的不同走法有多少种

hdu 5318 The Goddess Of The Moon 矩阵高速幂

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 438    Accepted Submission(s): 150 Problem Description Chang'e (嫦娥) is

HDU 5318 The Goddess Of The Moon(矩阵快速幂详解)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 题面: The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 800    Accepted Submission(s): 349 Problem Description Chang'e (嫦

hdu 5318 The Goddess Of The Moon

The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 650 Problem Description Chang’e (嫦娥) is a well-known character in Chinese ancient mytholog

【矩阵快速幂】HDU 5318 The Goddess Of The Moon

通道 题意:n个数,A后缀和B的前缀相同,建边,问长度为m的有多少个. 思路:建图,完了 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include <vector> 4 5 using namespace std; 6 7 typedef long long ll; 8 9 const int MAX_N = 57; 10 const ll MOD = 1000000007; 11 12 typedef vector&l

矩阵快速幂 HDOJ 5318 The Goddess Of The Moon

题目传送门 1 /* 2 DP::dp[i][k] 表示选择i个字符串,最后一次是k类型的字符串,它由sum (dp[i-1][j]) (a[j], a[k] is ok)累加而来 3 矩阵快速幂:将n个字符串看成n*n的矩阵,如果匹配,矩阵对应位置为1.矩阵缩短递推dp时间,然后乘m-1次(dp[m][i])累加即可 4 注意去重 5 详细解释:http://blog.csdn.net/keshuai19940722/article/details/47111215 6 */ 7 #inclu

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t