当前位置: 首页 > news >正文

利用编程思维做题之判断回文字符串

1. 理解问题

我们需要设计一个程序来判断给定的字符串是否为回文字符串。回文字符串是指一个字符串与其逆序串相等。例如,“CSUSTSUSC”就是一个回文字符串。

2. 输入输出

  • 输入:一个字符串。
  • 输出:如果字符串是回文,打印“yes”;否则,打印“no”。

3. 数据结构

我们将使用顺序栈和链队列来实现该程序。栈用于保存字符串的字符,而队列用于逐个比较字符。

栈的定义:

#define MAX_SIZE 100

struct Stack {
    char data[MAX_SIZE]; // 栈的元素
    int top;             // 栈顶指针
};

// 栈的初始化
void initStack(struct Stack* stack) {
    stack->top = -1;
}

// 判栈空
int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

// 入栈
void push(struct Stack* stack, char value) {
    stack->data[++stack->top] = value;
}

// 出栈
char pop(struct Stack* stack) {
    return stack->data[stack->top--];
}
队列的定义:

struct Node {
    char data;          // 队列的元素
    struct Node* next;  // 指向下一个节点的指针
};

struct Queue {
    struct Node* front; // 队列前端
    struct Node* rear;  // 队列后端
};

// 队列的初始化
void initQueue(struct Queue* queue) {
    queue->front = queue->rear = NULL;
}

// 判队空
int isQueueEmpty(struct Queue* queue) {
    return queue->front == NULL;
}

// 入队
void enqueue(struct Queue* queue, char value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;
    
    if (isQueueEmpty(queue)) {
        queue->front = queue->rear = newNode;
    } else {
        queue->rear->next = newNode;
        queue->rear = newNode;
    }
}

// 出队
char dequeue(struct Queue* queue) {
    if (isQueueEmpty(queue)) return '\0'; // 返回空字符
    struct Node* temp = queue->front;
    char value = temp->data;
    queue->front = queue->front->next;
    free(temp);
    return value;
}

4. 制定策略

  1. 使用栈:将字符串的每个字符压入栈中。
  2. 使用队列:将字符串的每个字符入队。
  3. 比较:从栈中出栈的字符和队列中出队的字符逐一比较,如果全部相等,则该字符串为回文。

5. 实现代码

5.1 完整 C 代码

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

#define MAX_SIZE 100

// 栈的定义
struct Stack {
    char data[MAX_SIZE]; // 栈的元素
    int top;             // 栈顶指针
};

// 栈的初始化
void initStack(struct Stack* stack) {
    stack->top = -1;
}

// 判栈空
int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

// 入栈
void push(struct Stack* stack, char value) {
    stack->data[++stack->top] = value;
}

// 出栈
char pop(struct Stack* stack) {
    return stack->data[stack->top--];
}

// 队列的定义
struct Node {
    char data;          // 队列的元素
    struct Node* next;  // 指向下一个节点的指针
};

struct Queue {
    struct Node* front; // 队列前端
    struct Node* rear;  // 队列后端
};

// 队列的初始化
void initQueue(struct Queue* queue) {
    queue->front = queue->rear = NULL;
}

// 判队空
int isQueueEmpty(struct Queue* queue) {
    return queue->front == NULL;
}

// 入队
void enqueue(struct Queue* queue, char value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;
    
    if (isQueueEmpty(queue)) {
        queue->front = queue->rear = newNode;
    } else {
        queue->rear->next = newNode;
        queue->rear = newNode;
    }
}

// 出队
char dequeue(struct Queue* queue) {
    if (isQueueEmpty(queue)) return '\0'; // 返回空字符
    struct Node* temp = queue->front;
    char value = temp->data;
    queue->front = queue->front->next;
    free(temp);
    return value;
}

// 判断字符串是否为回文
int isPalindrome(const char* str) {
    struct Stack stack;
    struct Queue queue;
    initStack(&stack);
    initQueue(&queue);

    int length = strlen(str);

    // 入栈和入队
    for (int i = 0; i < length; i++) {
        push(&stack, str[i]);
        enqueue(&queue, str[i]);
    }

    // 比较
    for (int i = 0; i < length; i++) {
        if (pop(&stack) != dequeue(&queue)) {
            return 0; // 不是回文
        }
    }

    return 1; // 是回文
}

// 主程序
int main() {
    const char* str = "CSUSTSUSC";
    if (isPalindrome(str)) {
        printf("yes\n");
    } else {
        printf("no\n");
    }

    return 0;
}

5.2 代码说明

  • *isPalindrome(const char str)**:判断字符串是否为回文。利用栈和队列分别存储字符,然后比较它们。
  • 主程序:调用判断函数并根据返回值输出结果。

5.3 模拟过程

以字符串 “CSUSTSUSC” 为例:

  1. 入栈和入队

    • 栈:[C, S, U, S, T, S, U, S, C]
    • 队列:[C, S, U, S, T, S, U, S, C]
  2. 比较

    • 从栈出栈 C,队列出队 C,匹配。
    • 从栈出栈 S,队列出队 S,匹配。
    • 依此类推,直到比较完毕。
  3. 结果

    • 所有字符匹配,字符串为回文,输出“yes”。

6. 运行结果

对于输入字符串 “CSUSTSUSC”,运行结果为:yes。

7. 时间和空间复杂度分析

  • 时间复杂度:O(n),其中 n 为字符串的长度。我们遍历字符串的每个字符一次。
  • 空间复杂度:O(n),栈和队列各需存储字符串的每个字符。


http://www.mrgr.cn/news/59785.html

相关文章:

  • 使用教程:基于 uiautomator2 和 pytest 的图片相似度测试脚本
  • Github优质项目推荐(第八期)
  • 基于树型结构实现顺序结构堆
  • Spring WebFlux学习笔记(一)
  • 设计模式4-工厂模式策略模式
  • react-query用户哭了:token认证还能这么玩?
  • 第13次CCF CSP认证真题解
  • 【设计模式系列】迭代器模式
  • XXE进阶
  • 前缀和算法 | 计算分矩阵的和
  • 【Chapter 11】中断时间序列分析:政策变化的因果推断
  • 【Chapter 5】因果推断中的倾向得分和双重稳健估计
  • Sampling采样与Virtual Columns虚拟列
  • 2024年最新Java毕业设计选题题目参考,2000+ Java毕业设计题目,值得收藏
  • 使用Python进行办公楼电能消耗数据的机器学习分析与预测
  • 【Qt】系统相关——多线程、Qt多线程介绍、常用函数、线程安全、网络、UDP Socket、TCP Socket
  • 2024年汽车修理工(高级)证模拟考试题库及汽车修理工(高级)理论考试试题
  • 逆向破解真随机数系统的思路
  • Axure设置文本——元件动作三
  • 算法|牛客网华为机试10-20C++
  • mysql中的视图表
  • 【Python】Python字典深入剖析:哈希映射与常见操作
  • 120.WEB渗透测试-信息收集-ARL(11)
  • 【golang】 lo.Map使用
  • 202.快乐数
  • ts:数组的常用方法(forEach、map)