POJ 2887

#include <iostream>
#include <string>
#define MAXN 2000
using namespace std;

struct node
{
	char c;
	int place;
};

node _node[MAXN];

int index;
void fun_q(int place);
int len;
string s;
int main()
{
	//freopen("acm.acm","r",stdin);

	int op;
	char c;
	char c_1;
	int q_place;
	int i_place;
	cin>>s;
	cin>>op;
	len = s.length();
	index = 0;

	while(op --)
	{
		cin>>c;
		if(c == ‘Q‘)
		{
			cin>>q_place;
			fun_q(q_place);
		}

		else if(c == ‘I‘)
		{
			cin>>c_1;
			cin>>i_place;
			if(i_place > len)
			{
				_node[index].c = c_1;
				_node[index].place = len+1;
				++ index;
				++ len;
			}
			else
			{
				++ len;
				_node[index].c = c_1;
				_node[index].place = i_place;
				++ index;
			}
		}

	}
}

void fun_q(int place)
{

	int i;
	int p = place;
	for(i = index-1; i >= 0; -- i)
	{
		//	cout<<p<<"))))))))))))) "<<endl;
		if(_node[i].place < p)
		{
			-- p;
		}
		else if(_node[i].place == p)
		{
			cout<<_node[i].c<<endl;
			return;
		}
	}
	cout<<s[p-1]<<endl;
}
时间: 2024-11-08 23:08:18

POJ 2887的相关文章

POJ 2887 Big String (块状数组)

题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每次插入肯定会超时. 而操作数比较少,考虑使用分块法.假设原字符串长度为L,则取每块长度l=sqrt(L).这样每次插入,我们需要用sqrt(L)的时间找到对应的块,再用sqrt(L)在该块进行插入.查询同样需要sqrt(L)找到该块,如果用数组实现可以O(1)找到目标元素.(我尝试用stl链表来做,结果超时了

poj 2887 Big String

题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do some string manipulations. Input The first line of the input contains the initial string. You can assume that it is non-empty and its length does not

poj 2887 块状链表

块状链表第一题,懒得写成链表形式的,所以写成了静态链表. 思想还是很简单的,就是进行分块查找和插入,和建立索引有点像,复杂度是根号的,实现起来比较容易,由于这个题插入操作不多,所以没有写split函数, 因为没有删除操作,所以也没有写union函数,随后有空再补上吧. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namesp

(块状链表) poj 2887

Big String Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 5936   Accepted: 1397 Description You are given a string and supposed to do some string manipulations. Input The first line of the input contains the initial string. You can ass

好题 线段树对数据的保存+离线的逆向插入 POJ 2887

题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后的位置,然后要把这个位置给保存下来在O(1)查询即可. //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <cstdio> #include<cstring> #include<algorithm> #include<

POJ 2887 Big String 线段树 离线处理

一开始看的时候没什么思路,后来一看卧槽不是简单的离线处理么.反着插入一遍然后直接查询就好了. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r const int maxq = 2e3 + 10; const

块状链表题*1

poj 2887 #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<string> #include<cstdio> #include<queue> #include<cmath> #define N 1000010 #define M 2005 using namespace std; cla

Poj 2887-Big String Splay

题目:http://poj.org/problem?id=2887 Big String Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 6527   Accepted: 1563 Description You are given a string and supposed to do some string manipulations. Input The first line of the input contai

POJ 3723 Conscription (最大权森林 + Kruskal算法)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8317   Accepted: 2887 Description Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be