异或树【C语言】

解题思路

复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_N 100005
#define MAX_M 1001
#define INF 0x3f3f3f3f
int a[MAX_M][5];
char s[MAX_N][10];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",s[i]);
    }
    for(int i=1;i<=m;i++){
        for(int j=0;j<5;j++){
            a[i][j]=-1;
        }
    }
    int min_len=INF;
    int L=0,R=0;
    for(int r=1;r<=n;r++){
        char* str=s[r];
        int j=str[0]-'A';
        int i=atoi(&str[1]);
        a[i][j]=r;
        int flag=1;
        int min_pos=INF;
        for(int k=0;k<5;k++){
            if(a[i][k]==-1){
                flag=0;
                break;
            }
            if(a[i][k]<min_pos){
                min_pos=a[i][k];
            }
        }
        if(flag){
            int len=r-min_pos+1;
            if(len<min_len||(len==min_len&&min_pos<L)){
                min_len=len;
                L=min_pos;
                R=r;
            }
        }
    }
    printf("%d %d\n",L,R);
    return 0;
}
相关推荐
铅笔小新z8 小时前
【C语言】操作符
c语言
by2099910 小时前
从 malloc/free 到 new/delete:真正理解 C++ 中的内存管理与对象生命周期
c语言·c++·经验分享·动态内存管理
额额额对了10 小时前
SPI通信
linux·c语言·汇编·单片机·嵌入式硬件·arm
Logic10111 小时前
C语言/数据结构位运算题解:异或XOR找出书柜中编号只出现一次的那一层——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Logic10113 小时前
C语言/数据结构位运算题解:异或XOR找出星球探险中的“独特能量石“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Escalating_xu14 小时前
【C 语言】深入理解指针(3):字符指针、数组指针、二维数组传参、函数指针与转移表
java·c语言·开发语言
垆边人似月.14 小时前
华为机考题:最小代价爬楼梯
c语言·c++
垆边人似月.15 小时前
华为机考题:二叉树的最大路径和
c语言·c++
程序猿编码15 小时前
不用 PyTorch!纯 C 实现 N 维张量 + 反向传播,完成手写数字识别训练
c语言·人工智能·pytorch·神经网络·卷积神经网络·大模型推理
Navigator_Z16 小时前
LeetCode //C - 1269. Number of Ways to Stay in the Same Place After Some Steps
c语言·算法·leetcode