Codeforces Gym 101164 F. Letter

41 篇文章 0 订阅
9 篇文章 0 订阅

题意

We build the infinite sequence of uppercase letters (A-Z) which starts with single-letter strings (A, B, …, Z), continues with two-letter strings (AA, AB, …, AZ, BA, BB, …, BZ, …, ZZ), then three letter strings and so on. The same-length strings are ordered lexicographically. We are interested in finding which letter sits at a given index in the sequence.

即串为 ABC...ZAAABAC...ZXZYZZAAAAABAAC...ZZXZZYZZZ... 求第 index 个位置的字符

解题思路

由于串的结构恒定,故可判断:ABC...Z 长为 1 字符串组成的字符为 26 个,AAABAC...ZZ 长为 2 字符串组成的字符为 26×26×2 个…之后为 26×26×26×3 个…

首先可根据 index 判断该字符所处的为 k 长字符串组。

后根据其在 k 长字符串组中的位置判断其具体字符。

例如: 求 1000 位置的字符。 26<100026+26×26×2 ,故其属于 2 长字符串组,且在属于第 (1000-26)/2 个 二字符 (??) 组中。

其中 AA 是二字符组中第 0 个,(A-'A')*26+(A-'A')=0 . AB 为二字符组中第 1 个,(A-'A')*26+(B-'A')=1AC 为二字符组中第 2 个,(A-'A')*26+(C-'A')=2 ,通过此方法类似判断即可。

代码

#include<bits/stdc++.h>
using namespace std;
long long num[10] = {1};
int c[10];
int main()
{
    for(int i=1;i<10;i++)   num[i] = num[i-1] * 26;
    for(int i=1;i<10;i++)   num[i] = num[i] * i;
    for(int idx;scanf("%d", &idx)!=EOF;)
    {
        memset(c, 0, sizeof(c));
        for(int len=1;;len++)
        {
            if(idx >= num[len])  idx -= num[len];
            else {
                int index = idx / len;
                for(int i=len-1;index;i--)
                    c[i] = index%26,
                    index /= 26;
                printf("%c\n", char(c[idx%len] + 'A'));
                break;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值