当前位置:网站首页>Guess The String (二分,交互)
Guess The String (二分,交互)
2022-07-17 05:13:00 【lovesickman】
Guess The String
题面翻译
本题为交互题,使用 IO 交互。
在你输出一行后,请清空缓冲区:
在 C++ 中,使用
fflush(stdout)或cout.flush()。在 Pascal 中,使用
flush(output)。在 Python 中,使用
stdout.flush()。其他语言请自行查阅文档。
请遵循题目完成交互,发出不合法询问可能会出现 TLE,WA 等问题。
给定一个长度为 n n n 且只包含小写字母的字符串 S S S,你需要猜出它。
一共有 4 4 4 种交互方式:
| 格式 | 允许调用次数 | 限制 | 返回值 | 说明 |
|---|---|---|---|---|
| 无 | 1 1 1 | 无 | 一个整数, n n n 的值。 | 在最开始调用。 |
? 1 i | 26 26 26 | i i i 为 [ 1 , n ] [1,n] [1,n] 范围内的整数。 | 一个字符, S i S_i Si( S S S 的第 i i i 个字符)。 | 无 |
? 2 l r | 6 × 1 0 3 6 \times 10^3 6×103 | 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1≤l≤r≤n,且 l , r l,r l,r 为整数。 | 一个整数, S l … r S_{l \ldots r} Sl…r( S S S 的第 l l l 至 r r r 个字符)中不同字符的种数。 | 无 |
! s | 1 1 1 | s s s 是一个字符串,代表你所认为的 S S S。 | (评测结果——AC 或 WA。) | 最后调用,然后停止交互。 |
对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 1 0 3 1 \le n \le 10^3 1≤n≤103。
样例 #1
样例输入 #1
5
4
u
2
g
e
s
1
样例输出 #1
? 2 1 5
? 1 2
? 2 1 2
? 1 1
? 1 3
? 1 4
? 2 4 5
! guess
分析可知:时间复杂度必为 O ( n l o g ( 26 ) ) O(nlog(26)) O(nlog(26)) , l o g ( 26 ) log(26) log(26) 意味着对26个字母的某种信息进行二分,并且该信息具有两段性。
i i i 从 1 ∼ n 1 \sim n 1∼n 进行循环,对 i = = 1 i == 1 i==1 特判,vector<int>v,存储每个字母最后一次出现的位置,当遇到以前遇到过的元素的时候,对所有已经出现过的字母的集合 v v v 进行二分。
注意的一点, v e c t o r vector vector下标从0开始,字符串从1开始,需要增加一位偏移量。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+7;
typedef long long ll;
// 2022-7-10 09:40:10
char query1(int i){
cout<<"? 1 "<<i<<endl;
char s;cin>>s;
return s;
}
int query2(int l,int r){
cout<<"? 2 "<<l<<" "<<r<<endl;
int s;cin>>s;
return s;
}
int n,cnt;
char ans[1010];
int node[30];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
if(i==1){
char t = query1(i);
ans[i] = t;
cnt++;
node[ans[i]-'a'] = i;
}
else{
int t = query2(1,i);
if(t == cnt+1){
char s = query1(i);
ans[i] = s;
cnt++;
node[ans[i]-'a']=i;
}
else{
// i位置字符和之前某个字符相同
vector<int>v;
for(int i=0;i<26;i++){
if(node[i]){
v.push_back(node[i]);
}
}
sort(v.begin(),v.end());
int l = 0, r = v.size()-1;
while(l<r){
int mid = l+r+1>>1;
if(query2(v[mid],i) == cnt-mid){
// cout<<"?"<<endl;
l = mid;
}
else
r = mid-1;
}
ans[i] = ans[v[l]];
node[ans[i]-'a']=i;
}
}
}
cout<<"! ";
for(int i=1;i<=n;i++){
cout<<ans[i];
}
return 0;
}
边栏推荐
- 配置VsCode中‘log’快捷键去掉console.log(‘‘);中的分号;
- High voltage module isolation module hra2460d-2w
- c语言调用文件浏览器,实现选择文件的效果
- 比例阀放大板1A、2A、3A、5A比例阀驱动模块0-10V转0-24V
- Wireless charging chip IC
- DSL实现Bucket聚合
- Material and application circuit diagram of 0-10V, 4-20mA current voltage to PWM isolation converter
- 为什么方案商“钟情”选择CS创世SD NAND
- Acwing第58场周赛(AK)
- busybox 指定日期修改 暂时不需要clock -w 写入硬件
猜你喜欢

处理中文分词 ik分词器以及拓展和停止字典

MySQL Workbench基本使用 【创建一个数据表】

Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme

Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;

【简单快速】启动后桌面正常下方任务栏无反应/鼠标一直转圈

结合图片看常用串口通信UART

0-10V,4-20mA电流电压转PWM隔离转换器 质料以及应用电路图

自动补全 & (自定义)拼音分词器 & 搜索时注意事项

解决Cannot read properties of null (reading ‘pickAlgorithm‘)

CS品牌SDNAND和STM32MCU成功合作资料
随机推荐
mapping索引属性 & 创建索的操作
General review of software process and management
MySQL workbench basically uses [create a database]
压力应变桥信号处理光电隔离放大器
Darwin's analytical experience
RS-485/232转4-20mA/0-10V隔离D/A转换器
Conversion, isolation and transmission of international standard signals 0-5v/0-10v/1-5v, 0-10ma/0-20ma/4-20ma, etc
[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning
5V boost charging 8.4v chip
ES聚合分析报错:“reason“ : “Text fields are not optimised for operations
3.7V锂电池升压到5V1A,FS2114升压转换芯片设计布局
分享CS品牌SDNAND与可穿戴设备之间成功合作
DAC7512N 模拟混合信号IC转换器
DSL实现自动补全查询
数字信号隔离模块 ADUM1401ARWZ 亚德诺 库存现货
2022-7-15 廉价国产PLC工控板带485主从通信的零散记录
Lithium battery charging management chip
MEX and Increments
Wireless charging chip IC
索引库中的文档的操作