poj_2945 Find the Clones (Trie树 内存分配)

【题目描述】

Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship orbiting around earth. After some (quite unpleasant) human experiments, the aliens cloned the victims, and released multiple copies of them back in Doubleville. So now it might happen that there are 6 identical person named Hugh F. Bumblebee: the original person and its 5 copies. The Federal Bureau of Unauthorized Cloning (FBUC) charged you with the task of determining how many copies were made from each person. To help you in your task, FBUC have collected a DNA sample from each person. All copies of the same person have the same DNA sequence, and different people have different sequences (we know that there are no identical twins in the town, this is not an issue).

【题目要求】

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 20000 people, and the length 1 ≤ m ≤ 20 of the DNA sequences. The next n lines contain the DNA sequences: each line contains a sequence of m characters, where each character is either `A', `C', `G' or `T'. 
The input is terminated by a block with n = m = 0 .

For each test case, you have to output n lines, each line containing a single integer. The first line contains the number of different people that were not copied. The second line contains the number of people that were copied only once (i.e., there are two identical copies for each such person.) The third line contains the number of people that are present in three identical copies, and so on: the i -th line contains the number of persons that are present in i identical copies. For example, if there are 11 samples, one of them is from John Smith, and all the others are from copies of Joe Foobar, then you have to print `1' in the first andthe tenth lines, and `0' in all the other lines.

【样例输入】

9 6
AAAAAA
ACACAC
GTTTTG
ACACAC
GTTTTG
ACACAC
ACACAC
TCCCCC
TCCCCC
0 0

【样例输出】

1
2
0
1
0
0
0
0
0

【我的解法】

这依然是一道典型的Trie树应用题,我们这里想要讨论的是:由于题目中有大量的输入block,对于每个block都需要重新建一棵新的Trie树,如果采用动态内存分配来实现,最后如果不加以释放,是否会占用大量内存空间而导致题目无法通过?而如果我们每一次都释放用过的空间,无疑需要遍历整棵树,如果block的数目很大,又有可能在时间上无法满足要求。

因此,我们决定采用静态内存分配,开定一个可以存储最多20000*20=400000个结点的数组,用来重复存储每一次新建的Trie树。

程序如下:

#include <stdio.h>

typedef struct
{
    char c;
    int num;
    long int fChild,rCousin;
} node;

void newNode(node tree[], long int p, char ch)
{
    tree[p].c=ch;
    tree[p].num=0;
    tree[p].fChild=-1;
    tree[p].rCousin=-1;
}

void print(node tree[], long int p, int a[])
{
    a[tree[p].num]++;
    if (tree[p].fChild>0) print(tree,tree[p].fChild,a);
    if (tree[p].rCousin>0) print(tree,tree[p].rCousin,a);
}

int main()
{
    node tree[400001];
    int m,n;
    scanf("%d%d",&n,&m);
    while (n>0)
    {
        long int total=0,i,j,k;
        char s[25];
        newNode(tree,0,'0');
        for (i=0;i<n;i++)
        {
            scanf("%s",s);
            long int x=0,y;
            for (j=0;j<m;j++)
            {
                y=tree[x].fChild;
                if (y==-1)
                {
                    for (k=j;k<m;k++)
                    {
                        total++;
                        newNode(tree,total,s[k]);
                        tree[x].fChild=total;
                        x=total;
                    }
                    break;
                }
                while (y!=-1)
                {
                    if (tree[y].c==s[j]){x=y;break;}
                    x=y;
                    y=tree[y].rCousin;
                }
                if (y==-1)
                {
                    total++;
                    newNode(tree,total,s[j]);
                    tree[x].rCousin=total;
                    x=total;
                    for (k=j+1;k<m;k++)
                    {
                        total++;
                        newNode(tree,total,s[k]);
                        tree[x].fChild=total;
                        x=total;
                    }
                    break;
                }
            }
            tree[x].num++;
        }
        int a[20001]={0};
        print(tree,0,a);
        for (i=1;i<=n;i++) printf("%d\n",a[i]);
        scanf("%d%d",&n,&m);
    }
    return 0;
}
继而,我又写了一个动态内存分配的程序,且每次都不释放空间,用以换取时间,来测试一下是否能通过评测。

程序如下:

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    char c;
    int num;
    struct node* fChild;
    struct node* rCousin;
}* triTree;

triTree newTree(char ch)
{
    triTree p=(triTree) malloc(sizeof(struct node));
    p->c=ch;
    p->num=0;
    p->fChild=NULL;
    p->rCousin=NULL;
    return p;
}

void print(triTree t, int a[])
{
    a[t->num]++;
    if (t->fChild!=NULL) print(t->fChild,a);
    if (t->rCousin!=NULL) print(t->rCousin,a);
}

int main()
{
    int m,n,i,j,k;
    char s[25];
    scanf("%d%d",&n,&m);
    while (n>0)
    {
        triTree t=newTree('0');
        for (i=0;i<n;i++)
        {
            scanf("%s",s);
            triTree x=t,y;
            for (j=0;j<m;j++)
            {
                y=x->fChild;
                if (y==NULL)
                {
                    for (k=j;k<m;k++){x->fChild=newTree(s[k]); x=x->fChild;}
                    break;
                }
                while (y!=NULL)
                {
                    if (y->c==s[j]){x=y;break;}
                    x=y;
                    y=y->rCousin;
                }
                if (y==NULL)
                {
                    x->rCousin=newTree(s[j]);
                    x=x->rCousin;
                    for (k=j+1;k<m;k++){x->fChild=newTree(s[k]); x=x->fChild;}
                    break;
                }
            }
            x->num++;
        }
        int a[20001]={0};
        print(t,a);
        for (i=1;i<=n;i++) printf("%d\n",a[i]);
        scanf("%d%d",&n,&m);
    }
    return 0;
}
两个程序都通过了评测,结果如下(第一个为静态内存分配,第二个为动态内存分配):


可以看到,在面对需要多次建立大Trie树的情况时,静态分配不仅大大减少了内存的开销,而且在时间上也很有优势。

也许多次开辟新的内存单元,同时大量使用指针调用,远没有静态数组索引来得快。这是值得我们思考和探寻的问题。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要研究同配耦合的双层相依网络的韧性,可以采用如下步骤: 1. 生成随机的双层相依网络,包括邻接矩阵a3、节点状态矩阵和状态转移规则等。 2. 计算网络的初始韧性,即在网络没有受到攻击时能够承受的最大攻击强度。 3. 对网络进行攻击,例如随机选择一些节点进行攻击,或者根据节点的度中心性等指标选择攻击目标。在每次攻击后,更新节点状态矩阵,并重新计算网络的韧性。 4. 重复步骤3,直到网络完全瘫痪或者攻击强度达到一定值为止。 5. 分析网络的韧性曲线,得出网络的韧性指标,例如平均攻击强度、网络瘫痪点、网络的韧性指数等。 以下是一个 MATLAB 实现的示例代码: ```matlab % 生成随机的双层相依网络 n = 50; % 节点数 p = 0.2; % 连边概率 a1 = rand(n) < p; % 生成第一层网络的邻接矩阵 a2 = rand(n) < p; % 生成第二层网络的邻接矩阵 a3 = [a1, a2; a2, a1]; % 生成双层相依网络的邻接矩阵 % 初始化节点状态矩阵 status_matrix = zeros(n * 2, 4); % 每个节点有四种状态 for i = 1:n % 第一层网络节点的状态 status_matrix(i, 1) = 1; % 初始状态为正常 status_matrix(i, 2) = 0; % 故障时间 status_matrix(i, 3) = 0; % 失效时间 status_matrix(i, 4) = 0; % 失效节点编号 % 第二层网络节点的状态 status_matrix(i + n, 1) = 1; status_matrix(i + n, 2) = 0; status_matrix(i + n, 3) = 0; status_matrix(i + n, 4) = 0; end % 设置状态转移规则 for t = 1:1000 % 进行一千个时间步长的模拟 for i = 1:n * 2 % 遍历所有节点 switch status_matrix(i, 1) % 根据节点当前状态进行状态转移 case 1 % 正常状态节点不变 continue case 2 % 故障状态节点经过60s后可以被修复 if (t - status_matrix(i, 2)) >= 60 status_matrix(i, 1) = 1; % 将节点状态改为正常状态 end case 3 % 失效状态节点被移除 a3(i, :) = 0; a3(:, i) = 0; status_matrix(i, 4) = i; % 保存失效节点编号 case 4 % 退化状态节点可连的边比正常时减少一半 a3(i, :) = a3(i, :) & (rand(1, n * 2) > 0.5); a3(:, i) = a3(:, i) & (rand(n * 2, 1) > 0.5); end end end % 计算网络的初始韧性 initial_strength = calculate_strength(a3); % 攻击网络 attack_strength = 0.1; % 每次攻击的强度 while true % 随机选择一些节点进行攻击 attacked_nodes = randperm(n * 2, round(attack_strength * n * 2)); for i = 1:length(attacked_nodes) % 更新节点状态矩阵 if status_matrix(attacked_nodes(i), 1) == 1 % 如果节点是正常状态 status_matrix(attacked_nodes(i), 1) = 2; % 改为故障状态 status_matrix(attacked_nodes(i), 2) = t; % 记录故障时间 end end % 重新计算网络的韧性 current_strength = calculate_strength(a3); if current_strength < 0.5 * initial_strength % 如果网络已经瘫痪 fprintf('网络已经瘫痪!\n'); break; end if current_strength < 0.9 * initial_strength % 如果网络的韧性下降到一定程度 fprintf('网络的韧性下降到一定程度!\n'); break; end % 更新时间步长 t = t + 1; end % 计算网络的韧性 function strength = calculate_strength(a) strength = sum(sum(a)) / (size(a, 1) * (size(a, 1) - 1)); end ``` 需要注意的是,这只是一个简单的示例代码,实际应用中需要根据具体的问题进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值