当前位置:网站首页>Guess the string (dichotomy, interaction)
Guess the string (dichotomy, interaction)
2022-07-19 06:06:00 【lovesickman】
Guess The String
Topic translation
This topic is interactive , Use IO Interaction .
After you output a line , Please empty the buffer :
stay C++ in , Use
fflush(stdout)orcout.flush().stay Pascal in , Use
flush(output).stay Python in , Use
stdout.flush().Please consult the documentation for other languages .
Please follow the topic to complete the interaction , Sending an illegal inquiry may appear TLE,WA Other questions .
Given a length of n n n A string that contains only lowercase letters S S S, You need to guess it .
Altogether 4 4 4 Two ways of interaction :
| Format | Number of calls allowed | Limit | Return value | explain |
|---|---|---|---|---|
| nothing | 1 1 1 | nothing | An integer , n n n Value . | Call... At the beginning . |
? 1 i | 26 26 26 | i i i by [ 1 , n ] [1,n] [1,n] Range of integers . | One character , S i S_i Si( S S S Of the i i i Characters ). | nothing |
? 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, And l , r l,r l,r Integers . | An integer , S l … r S_{l \ldots r} Sl…r( S S S Of the l l l to r r r Characters ) Different characters in Species . | nothing |
! s | 1 1 1 | s s s Is a string , Represents what you think S S S. | ( Evaluation results ——AC or WA.) | Last call , Then stop the interaction . |
about 100 % 100\% 100% The data of , 1 ≤ n ≤ 1 0 3 1 \le n \le 10^3 1≤n≤103.
Examples #1
The sample input #1
5
4
u
2
g
e
s
1
Sample output #1
? 2 1 5
? 1 2
? 2 1 2
? 1 1
? 1 3
? 1 4
? 2 4 5
! guess
Analysis can be seen : The time complexity must be O ( n l o g ( 26 ) ) O(nlog(26)) O(nlog(26)) , l o g ( 26 ) log(26) log(26) Means yes 26 Some information of letters is bisected , And the information has two phases .
i i i from 1 ∼ n 1 \sim n 1∼n Cycle through , Yes i = = 1 i == 1 i==1 Special judgement ,vector<int>v, Store the last occurrence of each letter , When you encounter elements you have encountered before , For all the letters that have appeared v v v Do two points .
One thing to watch out for , v e c t o r vector vector Subscript from 0 Start , String from 1 Start , Need to add a bit offset .
#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 The position character is the same as a previous character
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;
}
边栏推荐
- 【力扣】翻转二叉树
- minio基础知识介绍
- 结合图片看常用串口通信UART
- 宽电压输入高电压输出 电压控制型
- [simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning
- golang高并发特性goroutine介绍
- Material and application circuit diagram of 0-10V, 4-20mA current voltage to PWM isolation converter
- 计算几何(2)
- 开源在线的MarkDown编辑器 --【Editor.md】
- QTSS回调例程
猜你喜欢

DSL实现自动补全查询

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

Proportional valve amplifier 1a, 2a, 3a, 5A proportional valve drive module 0-10V to 0-24v

Rs-485/232 to 4-20ma/0-10v isolated d/a converter

Chrome浏览器设置 【显示右上角 翻译语言图标】

2021-09-15

ES聚合分析报错:“reason“ : “Text fields are not optimised for operations

设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去

RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)

Antd is not defined
随机推荐
數學基礎課2_歐拉函數,線性篩,擴歐
2022/07/14 learning notes (day07) array
【力扣】环形链表 II
c语言调用文件浏览器,实现选择文件的效果
Low power LDO linear regulator IC
js变量提升
Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme
2022/07/09 第五小组 丁帅 学习笔记 day02
HM agent multi section lithium battery charging IC
带透明png转换成c数组
数学基础课2_欧拉函数,线性筛,扩欧
解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...
c语言 指定日期开始多少天 显示
QTSS常数
Acwing game 57 (AK)
Computational geometry (2)
Darwin's analytical experience
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
[详细教程安装][配置] VsCode中关于Eslint的辅助插件
嵌入式C语言重点(const、static、voliatile、位运算)