我对分治的理解:https://www.cnblogs.com/AKMer/p/9728574.html

题目传送门:https://www.luogu.org/problemnew/show/P3612

因为每次操作都会使原串增长一倍,并且后半部分与前半部分极其相似,所以我们可以将其分治。

因为是将原串的最后一个字符放到第一个来,其余字符全部后移一位,所以在后一半的第\(i\)位就是前一半的第\(i-1\)位,如果是后一半的第\(1\)位,那么就是前一半的第\(len\)位。我们每次分治可以将串长减少一半,所以只需要\(logn\)的时间就可以将询问的位数减小至最开始的原串长度以内,就可以直接出结果了。

时间复杂度:\(O(logn)\)

空间复杂度:\(O(1)\)

代码如下:

#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long ll n;
int len;
char s[35]; char find(ll id) {
if(id<=len)return s[id];//如果到初始串内了就直接返回答案
ll tmp=len;
while((tmp<<1)<id)tmp<<=1;//tmp就是前半部分的长度
ll new_id=id-tmp-1;if(!new_id)new_id=tmp;//new_id是id位在前半部的位置
return find(new_id);//递归去找
} int main() {
scanf("%s%lld",s+1,&n);
len=strlen(s+1);
printf("%c",find(n));//找第n为
return 0;
}

洛谷【P3612】[USACO17JAN]Secret Cow Code秘密奶牛码的更多相关文章

  1. 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...

  2. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  3. Secret Cow code(USACO)

    题目描述:zxyer为了防止他的标程被别人抄走,他在计算机中的rar文件上设置了一个密码,其中每一个密码都有一个初始字符串,字符串由大写字母组成,且长度不超过30位,每一个密码还有一个询问,询问为一个 ...

  4. 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

    P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...

  5. 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…

    P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...

  6. 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

    题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...

  7. 洛谷P2888 [USACO07NOV]牛栏Cow Hurdles

    题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...

  8. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  9. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

随机推荐

  1. iOS GPUImage 滤镜介绍

    这里直接引用官方描述: The GPUImage framework is a BSD-licensed iOS library that lets you apply GPU-accelerated ...

  2. as2解析json

    as2写的json解析,带容错,如果要做格式检查,得自己修改了,直接贴代码 //--------------------------------------------------json解析---- ...

  3. C语言之基本算法12—谁是冠军

    /* ================================================================== 题目:甲乙丙丁四人猜A,B,C,D,E,F6个人谁是冠军,甲 ...

  4. KVM虚拟化技术实战全过程

    今天准备开始.................... centos安装-kvm 教程: http://www.linuxidc.com/Linux/2017-01/140007.htm http:// ...

  5. CSS3自定义Checkbox特效

    在线演示 本地下载

  6. ubuntu commands mysql

    use table; //选择某个表 show tables; //展现有几个表 select * from table; //显示表内容

  7. 模拟C#的事件处理和属性语法糖

    1. [代码]SharpEvent.hpp /* * SharpEvent.hpp * *  Created on: 2014-5-5 *      Author: leoking *   Copyr ...

  8. spring.jar的下载地址

    http://repo.spring.io/release/org/springframework/spring/

  9. EAV模型

    了解EAV设计基本原理的最好方法就是理解行建模(row modelling,其中EAV是广义形式). 以一超市数据库为例,必须管理数以千计的产品和品牌,其中许多产品存在期很短暂.那么,显而易见,产品名 ...

  10. 四 Django框架,models.py模块,数据库操作——创建表、数据类型、索引、admin后台,补充Django目录说明以及全局配置文件配置

    Django框架,models.py模块,数据库操作——创建表.数据类型.索引.admin后台,补充Django目录说明以及全局配置文件配置 数据库配置 django默认支持sqlite,mysql, ...