POJ 2887 Big String
来源:互联网 发布:tomcat源码有必要 编辑:程序博客网 时间:2024/06/07 01:35
分析:给你一串字符,让你进行一系列插入和查询操作,查询时输出第p个字符,数组插入慢,链表查找快,于是我们用块状链表。
# include <stdio.h># include <malloc.h># include <string.h> typedef struct node { int len; char a[1005]; struct node *next; }Node; int max; Node *Create() { int i,n,start=0; char s[1000005]; Node *head=NULL,*p,*q; scanf("%s",s); n=strlen(s); max=n; while(start<n) { p=(Node *)malloc(sizeof(Node)); if(n-start>1000) p->len=1000; else p->len=n-start; for(i=1;i<=p->len;i++) p->a[i]=s[start+i-1]; p->next=NULL; if(head==NULL) head=p; else q->next=p; q=p; start+=p->len; } return head; } void Insert(Node *head) { int i,k; char c; Node *p; getchar(); scanf("%c %d",&c,&k); if(k>max) { k=max+1; while(head->next) { k-=head->len; head=head->next; } } else { while(k>head->len) { k=k-head->len; head=head->next; } for(i=head->len;i>=k;i--) head->a[i+1]=head->a[i]; } head->a[k]=c; (head->len)++; max++; if(head->len>1000) { p=(Node *)malloc(sizeof(Node)); p->len=1; p->a[1]=head->a[head->len]; (head->len)--; p->next=head->next; head->next=p; } } void query(Node *head) { int k; scanf("%d",&k); while(k>head->len) { k=k-head->len; head=head->next; } printf("%c\n",head->a[k]); } int main() { int t; char ch; Node *head=Create(); scanf("%d",&t); while(t--) { getchar(); scanf("%c",&ch); if(ch=='I') Insert(head); else query(head); } return 0; }
0 0
- poj 2887 Big String
- POJ 2887 Big String
- poj 2887 Big String
- poj 2887 Big String
- POJ 2887 Big String 笔记
- [POJ 2887] Big String [块状链表]
- 【POJ 2887】Big String 块状链表
- POJ 2887Big String(块状链表)
- 【POJ 2887】Big String(块状数组)
- poj 2887 Big String 块状链表
- Poj 2887 Big String(块状数组)
- POJ 2887 Big String 块状链表
- POJ 2887 Big String 线段树离线
- POJ 2887 Big String(线段树 离线处理)
- poj 2887 big string (块状链表)
- POJ 2887 Big String (线段树解法)
- Big String
- String的Big-Three
- jsp操作数据库,并将结果分页显示
- 系统架构领域的一些学习材料
- Windows下查看进程与线程的相关工具
- PHP Cookie管理
- WSDL手动生成WebService代理类的方法
- POJ 2887 Big String
- Nexys3学习手记1:写在前面的话
- 设置WordPress主题表单样式
- HDU 3123 GCC
- 【计蒜客系列】挑战难题10:寻找插入位置
- IOS UI设计模式 - 分页
- 构造方法-java学习
- java正则表达式
- 什么是dex文件