SOJ 1037

1037. Decorations

Constraints

Time Limit: 5 secs, Memory Limit: 32 MB

Description

The Sultan of Sylvania loves throwing parties, because that gives him a reason to decorate the palace. He particularly likes decorations called streamers made up of different beads strung together on a string and hung from the ceiling. Now, like most Sultans, he is very particular about everything, including these strung decorations. Specifically, he only likes certain combinations of beads to be used on the streamers. For example, if there are four different types of beads - A, B, C and D - the Sultan might say "It pleases his highness that only the combinations ABB, BCA, BCD, CAB, CDD and DDA appear in the streamers at tonight's party". This, needless to say, puts a severe limit on the number of different streamers possible. For example, if the length of the streamers was 5, then the only possible streams of beads would be BCABB and BCDDA (strings such as ABBCA could not be used because BBC is not an approved combination). Since the Sultan likes variety, it is important to know the total number of streamers possible, given a length and the current bead combinations which tickle the Sultan's fancy.

Input

Input will consist of multiple test cases. Each case will consist of two lines. The first line will contain three positive integers n, l and m, where n indicates the number of bead types, l is the length of the streamers and m indicates the number of bead combinations which the Sultan likes. The maximum values for n, l and m will be 26, 100 and 600, respectively. The next line will contain the m combinations. Each combination will be of the same length (between 1 and 10) and will be separated using a single space. All combinations will make use of only the uppercase letters of the alphabet. An input line of 0 0 0 will terminate input and should not be processed.

Output

For each test case, output a single line indicating the number of possible streamers. All answers will be within the range of a 32-bit integer.

Sample Input

4 5 6
ABB BCA BCD CAB CDD DDA
5 4 5
E D C B A
4 8 3
AA BB CC
0 0 0

Sample Output

2
625

3

这题可以将其抽象成树形结构。给出的s1,s2两个子串,若s1的后n-1个字符与s2的前n-1个字符相同,则s2可连接在s1后面,即s2是s1的子节点。先确定所给出的全部m个子串之间的连接关系,再进行动态规划。dp[i][j]表示总字符串长度为 i+length时以第j个子串为末尾的种数(length为每个子串的长度),设第j个子串的子节点为k,所以状态转移方程就为dp[i+1][k]+=dp[i][j],这样递推直到求出dp[I-length]的所有值。

// Problem#: 1037
// Submission#: 5055567
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
struct edge{ //LINK记录子节点,next记录下一个子节点在E数组中的下标 
    int LINK;
    int next;
};
edge E[600*600];//存放边节点 
int head[600]; //存放每个节点第一个子节点的下标,若无子节点则返回-1 
int n,I,m,length;//n为字母种类,I为要求字符串的长度,m为子串的种数,length为每个子串的长度 
string decoration[600];//用来存储所有子串 
int dp[600][600];//dp[i][j]表示当字符串长度为 i+length时以子串j为末尾的种数 
int Count;

inline bool cmp(string &A,string &B)//判断子串A和子串B能够相连,即B是不是A的子节点 
{
    for(int i=1;i<length;++i)
    {
        if(A[i]!=B[i-1])
            return false;
    }
    return true;
}
void addEdge(int a,int b)//若b是a的子节点,则在ab之间添加一条a指向b的边,head[a]保存a的第一个子节点的在E中的下标,若无子节点则head[a]为-1 
{
    E[Count].LINK=b;
    E[Count].next=head[a];
    head[a]=Count++;
}
void readData()
{
    Count=0;
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;++i)
    {
        cin>>decoration[i];
    }
    length=decoration[0].size();//获取子串的长度 
    for(int i=0;i<m;++i)//所有的子串两两比较看是否能构成一条边 
    {
        for(int j=0;j<m;++j)
        {
            if(cmp(decoration[i],decoration[j]))
            {
                addEdge(i,j);
            }
        }
    }
}

void DP()//动态规划 
{
    memset(dp,0,sizeof(dp));
    for(int i=0;i<m;++i)//dp[0][i]即长度与子串相同且以第i个子串结尾的字符串种数,自然是只有一种 
        dp[0][i]=1;
    for(int i=1;i<=I-length;++i)//进行递推,求出dp[I-length]行的各个数据 
    {
        for(int j=0;j<m;++j)
        {
            for(int k=head[j];k!=-1;k=E[k].next)
            {
                dp[i][E[k].LINK]+=dp[i-1][j];//状态转移方程 
            }
        }
    }
} 
int main()
{
    while(cin>>n>>I>>m&&n)
    {
        readData();
        if(length>I) cout<<0<<endl;//子串长度大于总串长度则输出0 
        else if(length==1) cout<<pow(m,I)<<endl;//若子串长度为1则总字符串的每个位置均有m种取法,所以总种数为m^I种
        else
        {
            DP();
            int ans=0;
            for(int i=0;i<m;++i)//要求总种数,所以将第I-length行的数字全部相加即可 
                ans+=dp[I-length][i];
            cout<<ans<<endl;
        }
    }
    return 0;
}                         


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值