【洛谷P1383 高级打字机】
题目描述
早苗入手了最新的高级打字机。最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧。
请为这种高级打字机设计一个程序,支持如下3种操作:
1.T x:在文章末尾打下一个小写字母x。(type操作)
2.U x:撤销最后的x次修改操作。(Undo操作)
(注意Query操作并不算修改操作)
3.Q x:询问当前文章中第x个字母并输出。(Query操作)
文章一开始可以视为空串。
输入输出格式
输入格式:
第1行:一个整数n,表示操作数量。
以下n行,每行一个命令。保证输入的命令合法。
输出格式:
每行输出一个字母,表示Query操作的答案。
输入输出样例
说明
【数据范围】
对于40%的数据 n<=200;
对于100%的数据 n<=100000;保证Undo操作不会撤销Undo操作。
<高级挑战>
对于200%的数据 n<=100000;Undo操作可以撤销Undo操作。
<IOI挑战>
必须使用在线算法完成该题。
生硬的讲解
这题我一看就不会,所以直接运用数组强行模拟栈,轻松拿下50Pts(然后就开始使劲颓废)
那么我们就先来讲一讲50Pts是如何拿下的吧QwQ
首先,看到这个题(前100%的数据点),果断想到了栈(这前100%和栈完全一样啊QwQ)
直接模拟
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<stack>
using namespace std;
#define maxn 100001
int top,t;
char st[maxn];
int main()
{
freopen("type.in","r",stdin);
freopen("type.out","w",stdout);
scanf("%d",&t);
while(t--)
{
char c1[],c2[];
int x;
scanf("%s",c1);
if(c1[]=='T')
{
scanf("%s",c2);
st[++top]=c2[];
}
if(c1[]=='U')
{
scanf("%d",&x);
top-=x;
}
if(c1[]=='Q')
{
scanf("%d",&x);
printf("%c\n",st[x]);
}
}
return ;
}
那么就行这样吧
(然而并没有完(Van~))
讲一下wz小姐姐坠强的分块正解吧(能力有限,只能就着代码硬干)
#include <cmath>
#include <cstdio>
#include <cstring>
template <class T>
inline void read(T &num) { //这个快读就比较硬核
bool flag = ;
num = ;
char c = getchar();
while ((c < '' || c > '') && c != '-')
c = getchar();
if (c == '-') {
flag = ;
c = getchar();
}
num = c - '';
c = getchar();
while (c >= '' && c <= '')
num = (num << ) + (num << ) + c - '', c = getchar();
if (flag)
num *= -;
}
inline void read(char &c) {//读字符专用读取
do {
c = getchar();
} while (c == ' ' || c == '\n' || c == '\r');
}
template <class T>
inline void output(T num) {
if (num < ) {
putchar('-');
num = -num;
}
if (num >= )
output(num / );
putchar(num % + '');
}
template <class T>
inline void outln(T num) {
output(num);
putchar('\n');
}
template <class T>
inline void outps(T num) {
output(num);
putchar(' ');
}
template <class T>
inline T max(T a, T b) {
return a < b ? b : a;
}
const int N = ;
int n, B;
struct node {
char *s;
int now, pc;
node *pre;
node() {
now = pc = ;
s = new char[B];
pre = NULL;
}
void copy(node *src) {
now = src->now;
pc = src->pc;
pre = src->pre;
for (int i = ; i < now; i++) {
s[i] = src->s[i];
}
}
} * head[N];
int lst;
void append(char x) {
if (head[lst]->now == B) {
head[++lst] = new node;
head[lst]->pre = head[lst - ];
head[lst]->pc = head[lst - ]->pc + ;
head[lst]->s[head[lst]->now++] = x;
} else {
head[++lst] = new node;
head[lst]->copy(head[lst - ]);
head[lst]->s[head[lst]->now++] = x;
}
}
char query(int x) {
int siz = head[lst]->now + head[lst]->pc * B;
x = siz - x + ;
if (x <= head[lst]->now) {
return head[lst]->s[head[lst]->now - x];
}
x -= head[lst]->now;
node *now = head[lst]->pre;
while (x > B) {
now = now->pre;
x -= B;
}
return now->s[B - x];
}
int main() {
read(n);
B = sqrt(n);
head[] = new node;
for (int i = ; i <= n; i++) {
char op;
read(op);
if (op == 'T') {
char x;
read(x);
append(x);
}
if (op == 'U') {
int x;
read(x);
head[lst + ] = head[lst - x];
lst += ;
}
if (op == 'Q') {
int x;
read(x);
putchar(query(x));
putchar('\n');
}
}
}
大概就是这样,具体的我也不是很清楚,感性理解一下吧
【洛谷P1383 高级打字机】的更多相关文章
- 洛谷 P1383 高级打字机==codevs 3333 高级打字机
P1383 高级打字机 18通过 118提交 题目提供者yeszy 标签倍增图论高级数据结构福建省历届夏令营 难度省选/NOI- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 早苗入手 ...
- 2021.07.02 P1383 高级打字机题解(可持久化平衡树)
2021.07.02 P1383 高级打字机题解(可持久化平衡树) 分析: 从可以不断撤销并且查询不算撤销这一骚操作可以肯定这是要咱建一棵可持久化的树(我也只会建可持久化的树,当然,还有可持久化并查集 ...
- P1383 高级打字机
P1383 高级打字机 主席树 一发主席树解决. 插入操作十分显然. 撤销操作复制前面的版本就行. 询问操作十分显然. #include<iostream> #include<cst ...
- 洛谷 P1383 codevs 3333 高级打字机
题目描述 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x.(t ...
- (luogu P1383)高级打字机
高级打字机 题目链接 https://www.luogu.org/problemnew/show/P1383 背景 无聊中.. 随便在luogu上rand到了一道题 从此走上不归路 主席树是我暑假的时 ...
- 【Luogu】P1383高级打字机
可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...
- 「Luogu P1383 高级打字机」
一道非常基础的可持久化数据结构题. 前置芝士 可持久化线段树:实现的方法主要是主席树. 具体做法 这个基本就是一个模板题了,记录一下每一个版本的字符串的长度,在修改的时候就只要在上一个版本后面加上一个 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- [洛谷P1196][NOI2002]银河英雄传说 - 带偏移量的并查集(1)
Description 公元五八〇一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发 ...
随机推荐
- 6-2 如何读写json数据
通过查看help(json.dump)和help(json.dumps)帮助信息,dump是将转换格式到文件对象,而dumps转换格式到字符串. 一.Json.dumps() Json.dumps() ...
- sqlserver 创建 aspstate的方法
找到路径 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 不同版本找不同版本的路径 在命令行执行命令 aspnet_regsql.exe -ssadd -s ...
- linux MySql 的主从复制部署
MySql 复制 mysql 复制:将某一台主机上的 Mysql 数据复制到其它主机(slaves)上,并重新执行一遍从而实现 当前主机上的 mysql 数据与(master)主机上数据保持一致的过程 ...
- CF 937
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- snmpwalk工具使用
snmpwalk是SNMP的一个工具,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. 在linux下使用snmpwalk工具,我们 ...
- Vscode中解决Html文件中不能打断点问题
Vscode中解决Html文件中不能打断点问题: 默认情况下,VSCode是不能在Html文件里打断点的,但是可以修改设置,依次打开:文件->首选项->设置,然后功能->调试-> ...
- Spring Boot jpa Service层实现代码
package com.fei.service.impl; import java.util.ArrayList; import java.util.List; import javax.persis ...
- 并行操作多个序列map
>>> def add1(a): return a + 1 >>> def add2(a,b): return a + b >>> def add ...
- SpringBoot---Kafka
1.实战 <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka --> <dependency> < ...
- day2 for,not,while,range
>>> def str_len(s): ... l = len(s) ... if l > 3: ... print("3") ... elif l < ...