111

本文介绍了一个C++程序,用于从输入的字符串中构建二叉搜索树,并统计各个字符串出现的频率。通过递归方式实现字符串的插入操作,最终输出每个字符串及其占总字符串数的比例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 5e4 + 10;
const int INF = (1 << 30);
int t, n, m, x, y, z, ans, sum;
int a[maxn], ha[maxn], vis[maxn], deg[maxn], dis[maxn], path[maxn];
struct node {
    char str[25];
    int cnt;
    node *l, *r;
};
node *inster(node *head, char *s) {
    if(!head) {
        head = new node;
        head->cnt = 1;
        strcpy(head->str, s);
        head->l = head->r = NULL;
    } else {
        int cmp = strcmp(head->str, s);
        if(cmp > 0) {
            head->l = inster(head->l, s);
        } else if(cmp < 0) {
            head->r = inster(head->r, s);
        } else {
            head->cnt++;
        }
    }
    return head;
}
void put(node *head) {
    if(head) {
        put(head->l);
        printf("%s %.2lf%c\n", head->str, head->cnt * 100.0 / n, '%');
        put(head->r);
    }
}
int main() {
    node *head = NULL;
    char str[25];
    scanf("%d", &n);
    getchar();
    for(int i = 1; i <= n; i++) {
        gets(str);
        int len = strlen(str);
        for(int j = 0; j < len; j++) {
            if(str[j] >= 'A' && str[j] <= 'Z') {
                str[i] += 32;
            }
        }
        head = inster(head, str);
    }
    put(head);
    getchar();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值