分析:给你一串字符,让你进行一系列插入和查询操作,查询时输出第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;
}