You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改C语言程序以计算间隔大于1的重复字符距离

修复字符重复距离计算的C程序问题

现有一段C语言程序,目标是实现:对输入字符串中的每个字符,若后面存在相同字符,计算当前字符与最近的后续相同字符的距离;若没有则显示0。目前程序仅能处理相邻重复的情况(比如输入hello时输出正确),但输入helol时结果不符合预期:

现有程序的输入输出

输入:hello
输出(正确):
h:0
e:0
l:1
l:0
o:0

输入:helol
当前错误输出:
h:0
e:0
l:0
o:0
l:0

预期正确输出:
h:0
e:0
l:2
o:0
l:0

要求:仅修改letters和report两个函数完成修复。


原程序代码

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

struct charact {
    char ch;
    int occurs;
    struct charact *next;
};

typedef struct charact Char;
typedef Char *ListofChar;
typedef Char *CharNode_ptr;

void letters(char name[50], ListofChar *chars_ptr);
void report(ListofChar chars);
Char *create_node(char ch);

int main(void) {
    char name[50];
    ListofChar chars = NULL;

    scanf("%49s", name);
    letters(name, &chars);
    report(chars);

    return 0;
}

Char *create_node(char ch) {
    CharNode_ptr newnode_ptr;
    newnode_ptr = malloc(sizeof(Char));
    newnode_ptr->ch = ch;
    newnode_ptr->occurs = 0;
    newnode_ptr->next = NULL;
    return newnode_ptr;
}

void letters(char name[50], ListofChar *lst_ptr) {
    int str_lenth = strlen(name);
    *lst_ptr = create_node(name[0]);
    CharNode_ptr current = *lst_ptr;

    for (int i = 1; i < str_lenth; i++) {
        
        CharNode_ptr new = create_node(name[i]);
        current->next = new;
        current = new;
        
    }
    
    return;
}

void report(ListofChar chars) {
    int apostasi = 0;
    int epanalipsi = 0;
    
    for (CharNode_ptr current = chars; current != NULL; current = current->next) {
        if (current->next != NULL && current->next->ch == current->ch) {
            apostasi++;
            epanalipsi = apostasi;
        } 
        else {
            apostasi = 0;
            epanalipsi = 0;
        }
        
        current->occurs = apostasi;
        printf("%c: %d\n", current->ch, current->occurs);
    }
}

修复方案(仅修改letters和report函数)

修改后的letters函数

void letters(char name[50], ListofChar *lst_ptr) {
    int str_length = strlen(name);
    // 处理空字符串的边界情况
    if (str_length == 0) {
        *lst_ptr = NULL;
        return;
    }
    *lst_ptr = create_node(name[0]);
    (*lst_ptr)->occurs = 0; // 存储当前字符在原字符串中的索引位置
    CharNode_ptr current = *lst_ptr;

    for (int i = 1; i < str_length; i++) {
        CharNode_ptr new = create_node(name[i]);
        new->occurs = i; // 给每个节点记录自身的索引
        current->next = new;
        current = new;
    }
}

修改后的report函数

void report(ListofChar chars) {
    // 遍历链表中的每个节点
    for (CharNode_ptr current = chars; current != NULL; current = current->next) {
        int distance = 0;
        CharNode_ptr next_node = current->next;
        // 从当前节点的下一个节点开始,向后查找最近的相同字符
        while (next_node != NULL) {
            if (next_node->ch == current->ch) {
                // 计算距离:后续节点的索引 - 当前节点的索引
                distance = next_node->occurs - current->occurs;
                break; // 找到最近的就停止遍历,保证距离是最小的
            }
            next_node = next_node->next;
        }
        current->occurs = distance;
        printf("%c: %d\n", current->ch, current->occurs);
    }
}

修复说明

  1. letters函数修改点:

    • 新增空字符串的边界处理,避免索引越界。
    • 不再将occurs字段初始化为0,而是存储每个字符在原字符串中的索引位置,为后续计算距离提供位置参考。
  2. report函数修改点:

    • 移除原有的仅检查相邻字符的逻辑,改为对每个节点向后遍历整个链表,寻找最近的相同字符。
    • 利用letters函数存储的索引值计算距离,找到最近的相同字符后立即停止遍历,保证结果是当前字符到后续最近重复字符的距离。
    • 最后更新节点的occurs字段为计算出的距离并输出。

内容的提问来源于stack exchange,提问作者ManosMax13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:25:01