题解 | #MP3光标位置#

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

看成对n取模的有限群,定义算子“移动”
区间看作群上定义的映射,在begin距离n过于近的时候判断光标的状态选择更新begin或者end
begin和end进入上一步判断的标准只和光标最后的状态有关

#include <stdio.h>
int begin,end;
int trans(char opch)
{
    if(opch=='U')return -1;
    else return 1;
}
int limitedg(int *opra,int n,int len)//有限群算子opra
{
    int step;
    step=0;
    int output;
    for(int i=0;i<len;i++)
    {
        step+=opra[i];
    }
    output=n*150+step;
    output=output%n;
    return output+1;//向+1映射一下,规则里没有第0首
}
void ismygroup(int *opra,int n,int len)
{
    begin=150*n,end=150*n+3;
    int step=150*n;
    for(int i=0;i<len;i++)
    {
        step+=opra[i];
       if(step<begin){begin-=1;end-=1;}
       if(step>end){begin+=1;end+=1;}      
    }
    if(n>4)
    {
    if(begin%n>=n-3)
    {
        if(step%n<=2){begin=1;end=4;}
        else if(step%n>=n-3){begin=n-3;end=n;}
    }
    else{begin=begin%n+1;end=end%n+1;}
    }
    else{begin=1; end=n;}
}
int main() {
    int n;scanf("%d",&n);
    char str[1001];scanf("%s",str);
    int len=strlen(str); 
    int opration[len];
    for(int i=0;i<len;i++)
    opration[i]=trans(str[i]);
    int output=limitedg(opration, n,len);
    ismygroup(opration,n,len);
    for(int i=begin;i<=end;i++)
    printf("%d ",i);
    printf("\n");
    printf("%d",output);
    return 0;
}

全部评论

相关推荐

01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
2025-12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务