1521
#include <stdio.h>
#define N 1000
void printMirror(int a[N+1][3], int i)
{
if (i == 0)
return ;
if (i != 1)
printf(" ");
printf("%d", a[i][0]);
printMirror(a, a[i][2]);
printMirror(a, a[i][1]);
}
int main(void)
{
int n, i;
int a[N+1][3];
char s[2];
while (scanf("%d", &n) != EOF)
{
if (n == 0)
{
printf("NULL\n");
continue;
}
for(i=1; i<=n; i++)
scanf("%d", &a[i][0]);
for(i=1; i<=n; i++)
{
scanf("%s", s);
switch(s[0])
{
case 'd':
scanf("%d%d", &a[i][1], &a[i][2]);
break;
case 'l':
scanf("%d", &a[i][1]);
a[i][2] = 0;
break;
case 'r':
scanf("%d", &a[i][2]);
a[i][1] = 0;
break;
case 'z':
a[i][1] = 0;
a[i][2] = 0;
break;
}
}
printMirror(a, 1);
printf("\n");
}
return 0;
}
1522
#include <stdio.h>
#include <stdlib.h>
typedef struct stack{
int num;
int min;
}Stack;
typedef struct arr{
Stack s[1000000];
}Arr;
int main(int argc, char const *argv[])
{
int n,i,m;
char c;
while(scanf("%d",&n)!=EOF && n>=1 && n<=1000000){
Arr *a = (Arr *)malloc(sizeof(Arr));
int top = 0;
for(i=0;i<n;i++){
scanf("\n%c",&c);
if(c == 's'){
scanf("%d",&m);
if(top == 0){
a->s[top].min = m;
}else{
a->s[top].min = (m<a->s[top-1].min?m:a->s[top-1].min);
}
a->s[top].num = m;
top++;
printf("%d\n",a->s[top-1].min);
}
else{
if(top > 1){
top--;
printf("%d\n",a->s[top-1].min);
}else{
top = 0;
printf("NULL\n");
}
}
}
}
return 0;
}
1523
#include <stdio.h>
#include <malloc.h>
#define N 1001
int n;
int a[N][4];
int create()
{
int i;
for (i=1; i<=n; i++)
scanf("%d", &a[i][0]);
char s[2];
for (i=1; i<=n; i++)
{
scanf("%s", s);
a[i][1] = a[i][2] = a[i][3] = 0;
if (s[0] == 'd')
{
scanf("%d%d", &a[i][1], &a[i][2]);
a[a[i][1]][3] = i;
a[a[i][2]][3] = i;
}
else if (s[0] == 'l')
{
scanf("%d", &a[i][1]);
a[a[i][1]][3] = i;
}
else if (s[0] == 'r')
{
scanf("%d", &a[i][2]);
a[a[i][2]][3] = i;
}
}
int root = 1;
while (a[root][3] != 0)
root = a[root][3];
return root;
}
typedef int Item;
typedef struct node * PNode;
typedef struct node
{
Item data;
PNode next;
}Node;
typedef struct
{
PNode front;
PNode rear;
int size;
}Queue;
Queue *InitQueue();
void DestroyQueue(Queue *pqueue);
void ClearQueue(Queue *pqueue);
int IsEmpty(Queue *pqueue);
int GetSize(Queue *pqueue);
PNode GetFront(Queue *pqueue,Item *pitem);
PNode GetRear(Queue *pqueue,Item *pitem);
PNode EnQueue(Queue *pqueue,Item item);
PNode DeQueue(Queue *pqueue,Item *pitem);
void QueueTraverse(Queue *pqueue,void (*visit)());
Queue *InitQueue()
{
Queue *pqueue = (Queue *)malloc(sizeof(Queue));
if(pqueue!=NULL)
{
pqueue->front = NULL;
pqueue->rear = NULL;
pqueue->size = 0;
}
return pqueue;
}
void DestroyQueue(Queue *pqueue)
{
if(IsEmpty(pqueue)!=1)
ClearQueue(pqueue);
free(pqueue);
}
void ClearQueue(Queue *pqueue)
{
while(IsEmpty(pqueue)!=1)
{
DeQueue(pqueue,NULL);
}
}
int IsEmpty(Queue *pqueue)
{
if(pqueue->front==NULL&&pqueue->rear==NULL&&pqueue->size==0)
return 1;
else
return 0;
}
int GetSize(Queue *pqueue)
{
return pqueue->size;
}
PNode GetFront(Queue *pqueue,Item *pitem)
{
if(IsEmpty(pqueue)!=1&&pitem!=NULL)
{
*pitem = pqueue->front->data;
}
return pqueue->front;
}
PNode GetRear(Queue *pqueue,Item *pitem)
{
if(IsEmpty(pqueue)!=1&&pitem!=NULL)
{
*pitem = pqueue->rear->data;
}
return pqueue->rear;
}
PNode EnQueue(Queue *pqueue,Item item)
{
PNode pnode = (PNode)malloc(sizeof(Node));
if(pnode != NULL)
{
pnode->data = item;
pnode->next = NULL;
if(IsEmpty(pqueue))
{
pqueue->front = pnode;
}
else
{
pqueue->rear->next = pnode;
}
pqueue->rear = pnode;
pqueue->size++;
}
return pnode;
}
PNode DeQueue(Queue *pqueue,Item *pitem)
{
PNode pnode = pqueue->front;
if(IsEmpty(pqueue)!=1&&pnode!=NULL)
{
if(pitem!=NULL)
*pitem = pnode->data;
pqueue->size--;
pqueue->front = pnode->next;
free(pnode);
if(pqueue->size==0)
pqueue->rear = NULL;
}
return pqueue->front;
}
void QueueTraverse(Queue *pqueue,void (*visit)())
{
PNode pnode = pqueue->front;
int i = pqueue->size;
while(i--)
{
visit(pnode->data);
pnode = pnode->next;
}
}
void list(int root, Queue *pq)
{
int count = 0;
EnQueue(pq, root);
while (!IsEmpty(pq))
{
DeQueue(pq, &root);
if (count >= 1)
printf(" ");
printf("%d", a[root][0]);
count ++;
if (a[root][1] != 0)
EnQueue(pq, a[root][1]);
if (a[root][2] != 0)
EnQueue(pq, a[root][2]);
}
printf("\n");
}
int main()
{
Queue *pq;
int root;
while(scanf("%d", &n) != EOF)
{
root = create();
pq = InitQueue();
list(root, pq);
}
return 0;
}
1524
int main(void)
{
int n, i, j;
int a[N+1][2];
while (scanf("%d", &n) != EOF)
{
for (j=0; j<2; j++)
{
a[0][j] = 0;
for(i=1; i<=n; i++)
scanf("%d", &a[i][j]);
}
for(i=1; i<=n; i++)
printf("%d %d\n", a[i][0], a[a[i][1]][0]);
}
return 0;
}
/**************************************************************
Problem: 1524
User: liangrx06
Language: C
Result: Accepted
Time:40 ms
Memory:912 kb
****************************************************************/
1525
#include <stdio.h>
#include <stdlib.h>
void func(char a[],int l,int r){
if(l == r){printf("%c",a[l]);}
else if(l<r){
int i,j,flag=0;
for(i=l;i<=r;i++){
if(a[i] == ' '){
flag =1;
j = i;
break;
}
}
if(flag == 1){
func(a,l,j-1);
printf(" ");
func(a,j+1,r);
}
else{
for(i=r;i>=l;i--){
printf("%c",a[i]);
}
}
}
return ;
}
int main()
{
int n;
while(scanf("%d",&n) != EOF){
if(n == 0 ) return 0;
if(n < 0) continue;
char a[n];
int i,j;
getchar();
gets(a);
int index = 0;
for(i=1;i<n;i++){
if((a[index] == ' ')&&a[i]==' '){
continue;
}
else a[++index] = a[i];
}
func(a,0,index);
printf("\n");
}
return 0;
}
/************************************************************** Problem: 1525 User: liangrx06 Language: C Result: Accepted Time:80 ms Memory:940 kb ****************************************************************/
1526
#include <stdio.h>
int friend[100001];
int rank[100001];
int getSet(int x)
{
while(x != friend[x])
x = getSet(friend[x]);
return x;
}
void merge(int x, int y)
{
x = getSet(x);
y = getSet(y);
if(x == y)
return;
if(rank[x] > rank[y])
{
friend[y] = x;
rank[x] += rank[y];
}
else
{
rank[y] += rank[x];
friend[x] = y;
}
}
int main()
{
int n,m,i,a,b,count=0;
while(scanf("%d",&n)!=EOF&&n!=0)
{
scanf("%d",&m);
for(i=1;i<=n;i++)
{
friend[i] = i;
rank[i] = 1;
}
while(m != 0)
{
m--;
scanf("%d%d",&a,&b);
merge(a,b);
}
count = 0;
for(i=1;i<=n;i++)
{
if(friend[i] == i)
count++;
}
printf("%d\n",count);
}
return 0;
}
1527
#include <stdio.h>
#define N 100000
void print(int a[], int n)
{
int i;
for (i=0; i<n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main(void)
{
int n, i;
int a[N], x[N], y[N];
int max, k;
int sum, t;
while (scanf("%d", &n) != EOF)
{
for(i=0; i<n; i++)
scanf("%d", &a[i]);
max = k = a[0];
for(i=1; i<n; i++)
{
k = (k<=0) ? a[i] : a[i]+k;
max = (k>max) ? k : max;
}
sum = x[0] = a[0];
for (i=1; i<n; i++)
{
sum += a[i];
x[i] = (sum>x[i-1]) ? sum : x[i-1];
}
sum = y[n-1] = a[n-1];
for (i=n-2; i>=0; i--)
{
sum += a[i];
y[i] = (sum>y[i+1]) ? sum : y[i+1];
}
for (i=0; i<n-1; i++)
{
t = x[i]+y[i+1];
max = (t>max) ? t : max;
}
if (max < 0)
max = 0;
printf("%d\n", max);
}
return 0;
}
1528
#include <stdio.h>
#include <string.h>
#define N 200000
int main(void)
{
int n, i, j, k;
char s[N+1];
int len[N];
while (scanf("%s", s) != EOF)
{
n = strlen(s);
for(i=0; i<n; i++)
{
j = i-1;
k = i+1;
while (j>=0 && k<n)
{
if (s[j] != s[k])
break;
j--;
k++;
}
len[i] = k-j-1;
j = i;
k = i+1;
while (j>=0 && k<n)
{
if (s[j] != s[k])
break;
j--;
k++;
}
if (k-j-1 > len[i])
len[i] = k-j-1;
}
int max = 0;
for (i=0; i<n; i++)
max = len[i]>max ? len[i] : max;
printf("%d\n", max);
}
return 0;
}
/************************************************************** Problem: 1528 User: liangrx06 Language: C Result: Accepted Time:40 ms Memory:1816 kb ****************************************************************/
1529
#include <stdio.h>
#define N 8
int n;
int islegal(int i, int j)
{
return i>=0 && i<n && j>=0 && j<n;
}
int main(void)
{
int i, j;
int a[N][N], b[N][N];
n = 8;
while (scanf("%d", &a[0][0]) != EOF)
{
for(i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0 && j == 0)
continue;
scanf("%d", &a[i][j]);
}
}
for(i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
int max = 0;
if (islegal(i-1, j))
max = (b[i-1][j] > max) ? b[i-1][j] : max;
if (islegal(i, j-1))
max = (b[i][j-1] > max) ? b[i][j-1] : max;
b[i][j] = a[i][j]+max;
}
}
printf("%d\n", b[n-1][n-1]);
}
return 0;
}
/************************************************************** Problem: 1529 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/
1530
int q[M];
int front, rear;
int flag[M];
void initQueue()
{
front = rear = 0;
memset(flag, 0, sizeof(flag));
}
int isEmpty()
{
return front == rear;
}
int isFull()
{
return front == (rear+1)%M;
}
int enQueue(int c)
{
if (isFull())
return 0;
q[rear] = c;
flag[c] = 1;
rear = (rear+1)%M;
return 1;
}
int deQueue(int *c)
{
if (isEmpty())
return 0;
*c = q[front];
flag[*c] = 0;
front = (front+1)%M;
return 1;
}
int lenQueue()
{
return (rear+M-front)%M;
}
int main(void)
{
int n, i;
char s[N+1];
int a, b;
int max;
while (scanf("%s", s) != EOF)
{
n = strlen(s);
initQueue();
max = 0;
for(i=0; i<n; i++)
{
a = s[i]-'a';
while (flag[a])
deQueue(&b);
enQueue(a);
//printf("%d %d\n", front, rear);
if (lenQueue() > max)
max = lenQueue();
}
printf("%d\n", max);
}
return 0;
}
/**************************************************************
Problem: 1530
User: liangrx06
Language: C
Result: Accepted
Time:20 ms
Memory:912 kb
****************************************************************/