zoj 1540 Censored! AC自动机+DP 长度为len的字符串不包含病毒串的个数

The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences.

But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1], S[k+1] = W[2], ..., S[k+len(W)-1] = W[len(W)], where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years.

Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it.


Input

The first line of the input file contains three integer numbers: N - the number of letters in Freish alphabet, M - the length of all Freish sentences and P - the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10).

The second line contains exactly N different characters - the letters of the Freish alphabet (all with ASCII code greater than 32).

The following P lines contain forbidden words, each not longer than min(M, 10) characters, all containing only letters of Freish alphabet.

Process to the end of file.


Output

Output the only integer number - the number of different sentences freelanders can safely use.


Sample Input

2 3 1
ab
bb
3 2 0
012
3 3 3
QWE
QQ
WEE
Q
2 50 4
AB
AA
AB
BA
BB


Sample Output

5
9
7
0


//


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=510;
struct Node
{
    int flag;//序号
    int id;//在静态链表中的位置
    Node* next[51];
    Node* fail;
};
Node temp[maxn];
int tp;
int n;
int len;
int kind;
int hash[200];
Node* root;
void reset(Node* p)
{
    p->flag=0;p->id=tp-1;
    for(int i=0;i<kind;i++) p->next[i]=NULL;
    p->fail=root;
    if(p==root) p->fail=NULL;
}
void init()
{
    tp=0;
    root=&temp[tp++];
    reset(root);
}
void insert(char* word)
{
    Node* p=root;
    for(int i=0;word[i];i++)
    {
        int x=hash[word[i]];
        if(p->next[x]==NULL)
        {
            p->next[x]=&temp[tp++];
            reset(p->next[x]);
        }
        p=p->next[x];
    }
    p->flag=1;
}
Node* que[maxn];
int front,rear;
void DFA()
{
    Node* p=root;
    front=rear=0;
    que[rear++]=p;
    while(front<rear)
    {
        Node* t=que[front++];
        for(int i=0;i<kind;i++)
        {
            Node* cnt=t->next[i];
            if(cnt!=NULL)
            {
                Node* fath=t->fail;
                while(fath!=NULL&&fath->next[i]==NULL)
                {
                    fath=fath->fail;
                }
                if(fath!=NULL)
                {
                    cnt->fail=fath->next[i];
                }
                else
                {
                    cnt->fail=p;
                }
                que[rear++]=cnt;
            }
        }
    }
}
void add(int *a,int *b)//a=a+b  从最低位到最高位 每一位存10000
{
int c[101];
int j=0;
for(int i=0;i<99;i++)
{
int k=a[i]+b[i]+j;
c[i]=k%10000;
j=k/10000;
}
for(int i=0;i<99;i++) a[i]=c[i];
}
void print(int *a)
{
int j=99;
while(j>=0&&a[j]==0) j--;
if(j==-1)
{
printf("0\n");
return;
}
printf("%d",a[j]);
  for(int i=j-1;i>=0;i--) printf("%04d",a[i]);
printf("\n");
}
int dp[51][501][100];
void toMatrix()
{
    Node* fath;
    memset(dp,0,sizeof(dp));
    for(int i=0;i<rear;i++)
    {
        Node* tmp=&temp[i];
        int mark=1;
        for(fath=tmp;fath!=NULL;fath=fath->fail)
        {
            if(fath->flag)
            {
                mark=0;
                break;
            }
        }
        if(mark) dp[0][i][0]=1;
    }
    for(int s=0;s<len;s++)//由于len比较小  故不用矩阵乘法
    {
        for(int i=0;i<rear;i++)
        {
            Node* p=&temp[i];
            if(p->flag) continue;
            for(int j=0;j<kind;j++)
            {
                Node* cnt=p->next[j];
                if(cnt!=NULL)
                {
                    int mark=1;
                    for(fath=cnt;fath!=NULL;fath=fath->fail)
                    {
                        if(fath->flag)
                        {
                            mark=0;
                            break;
                        }
                    }
                    if(mark)
                    {
                        int k=cnt->id;
                        add(dp[s+1][i],dp[s][k]);
                    }
                }
                else
                {
                    fath=p->fail;
                    while(fath!=NULL&&fath->next[j]==NULL)
                    {
                        fath=fath->fail;
                    }
                    if(fath!=NULL)
                    {
                        cnt=fath->next[j];
                        int mark=1;
                        for(fath=cnt;fath!=NULL;fath=fath->fail)
                        {
                            if(fath->flag)
                            {
                                mark=0;
                                break;
                            }
                        }
                        if(mark)
                        {
                            int k=cnt->id;
                            add(dp[s+1][i],dp[s][k]);
                        }
                    }
                    else
                    {
                        cnt=root;
                        add(dp[s+1][i],dp[s][0]);
                    }
                }
            }
        }
    }
}
int main()
{
    char str[60];
    while(scanf("%d%d%d",&kind,&len,&n)==3){
    scanf("%s",str);
    for(int i=0;str[i];i++) hash[str[i]]=i;
    init();
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        insert(str);
    }
    DFA();
    toMatrix();
    print(dp[len][0]);
    }
    return 0;
}


//上面代码在POJ上超内存  下面代码内存占用比较小 在POJ上能够过

#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdio>
using std::cin;
using std::cout;
using std::endl;
struct e{
int p[51];
int end;
}trie[501];


int n,m,K;
int hash[200];
int state=1;
int f[501];




void build(char c[]){
int i,j=0,k;
for(i=1;;)
{
if(trie[i].p[hash[c[j]]]==0)
trie[i].p[hash[c[j]]]=++state;
i=trie[i].p[hash[c[j]]];
j++;
if(j>=strlen(c))
break;
}
trie[i].end=1;
}


void ac(){
int q[501];
int l=0,r=0;
int i,j,k;
for(i=0;i<n;i++)
if(trie[1].p[i])
{
q[++r]=trie[1].p[i];
f[trie[1].p[i]]=1;
}
else
{
trie[1].p[i]=1;
}
while(l<r){
i=q[++l];


for(j=0;j<n;j++)
{
if(trie[i].p[j])
{
q[++r]=trie[i].p[j];
k=f[i];
while(!trie[k].p[j])
k=f[k];
f[trie[i].p[j]]=trie[k].p[j];
trie[trie[i].p[j]].end|=trie[trie[k].p[j]].end;
}
}
}
}


int dp[51][501][100];


void plus(int *a,int *b){
int i,j=0,k;
int c[101];
for(i=0;i<99;i++)
{
k=a[i]+b[i]+j;
c[i]=k%10000;
j=k/10000;
}
for(i=0;i<99;i++)
a[i]=c[i];
}


void print(int *a){
int i,j,k;
j=99;
while(j>=0&&a[j]==0)
j--;
if(j==-1)
{
cout<<0<<endl;
return;
}
cout<<a[j];cout.fill('0');
  for(i=j-1;i>=0;i--)
{
cout.width(4);
cout<<a[i];
}
cout<<endl;
}




void loop(){
    memset(dp,0,sizeof(dp));
int i,j,k,s;
for(i=1;i<=state;i++)
if(trie[i].end==0)
dp[0][i][0]=1;
for(s=0;s<m;s++)
for(i=1;i<=state;i++)
if(trie[i].end)
continue;
else
{
for(j=0;j<n;j++)
if(trie[i].p[j])
{
k=trie[i].p[j];
if(trie[k].end==0)
plus(dp[s+1][i],dp[s][k]);
}
else
{
k=f[i];
while(!trie[k].p[j])
k=f[k];
if(!trie[trie[k].p[j]].end)
{
k=trie[k].p[j];
plus(dp[s+1][i],dp[s][k]);
}
}
}
}






int main(){
int i,j,k;
char c[60];
while(cin>>n>>m>>K){
memset(trie,0,sizeof(trie));
state=1;
cin>>c;
for(i=0;i<strlen(c);i++)
hash[c[i]]=i;
for(i=1;i<=K;i++)
{
cin>>c;
build(c);
}
ac();
loop();
print(dp[m][1]);
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将RSA公钥表示成字符串的形式,在进行加密时需要先将其转换成公钥结构体类型,然后再使用mbedtls库提供的接口进行加密。 以下是一个示例代码,假设公钥数据已经以字符串的形式存储在`public_key_str`中: ```c #include "mbedtls/rsa.h" #include "mbedtls/pk.h" // RSA公钥字符串 const char *public_key_str = "-----BEGIN PUBLIC KEY-----\n" "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw7Yj9RbqL6pG9Sv3GnZg\n" "h0T3Wz5c0XeX5G8tF/NVbzXUEDRqOZkdt3jv5+0fV5hX6gDZGxhW2k6B9GjPf4XQ\n" "TFYFhCjG5+Y2u4kR7BF0/3KzVcDKMxYmKo7rj2y5OvI5u5zPj6VhKRMoV9JNv5oM\n" "gGpZoF2LwRjuzl7a2MfPZz3+J8+htnLWlVwJyvGn1+sIi3rY9yjV9I6X1hPcqKb8\n" "0KaD0B20qRb4H7rG9yf9FwK7DcC+Zf9jJv4nI8UvRhQ/2zOj9fvy/1aRDN3qQ3Sf\n" "cLJ7fW1dK2RzB5E5XfJZkJwvZVfLHt/1QZGjz5QIg4sB9Xc2zQXmZL+LwN0CF0k+\n" "iwIDAQAB\n" "-----END PUBLIC KEY-----\n"; // 加密函数 int rsa_encrypt(const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen) { int ret = 0; mbedtls_pk_context pk; mbedtls_rsa_context *rsa = NULL; // 初始化公钥结构体 mbedtls_pk_init(&pk); // 解析公钥字符串,填充到公钥结构体 ret = mbedtls_pk_parse_public_key(&pk, (const unsigned char *)public_key_str, strlen(public_key_str) + 1); if (ret != 0) { mbedtls_pk_free(&pk); return ret; } // 获取RSA公钥结构体 rsa = mbedtls_pk_rsa(pk); if (rsa == NULL) { mbedtls_pk_free(&pk); return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; } // 进行加密操作 ret = mbedtls_rsa_pkcs1_encrypt(rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, ilen, input, output); if (ret != 0) { mbedtls_pk_free(&pk); return ret; } // 获取输出数据长度 *olen = mbedtls_rsa_get_len(rsa); // 释放资源 mbedtls_pk_free(&pk); return 0; } // 主函数 int main(void) { unsigned char input[] = "Hello, world!"; unsigned char output[256] = {0}; size_t olen = 0; int ret = 0; // RSA加密 ret = rsa_encrypt(input, sizeof(input) - 1, output, &olen); if (ret != 0) { printf("RSA encrypt error: %d\n", ret); return ret; } // 输出加密结果 printf("Encrypted message: "); for (size_t i = 0; i < olen; i++) { printf("%02X ", output[i]); } printf("\n"); return 0; } ``` 在上述代码中,`rsa_encrypt`函数用于进行RSA加密,输入数据为`input`,输入数据长度为`ilen`,输出数据为`output`,输出数据长度为`olen`。`main`函数中,调用`rsa_encrypt`函数进行加密,将加密结果输出到控制台上。 需要注意的是,在进行加密操作时,需要对输出缓冲区进行合适的大小分配。在上述代码中,输出缓冲区的大小为256字节,这个大小需要根据加密数据的长度和RSA密钥长度来进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值