小陈的开学第六周程序

一、Atcoder

1.Slimes

题意

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

1≤N≤10^5
|S|=N
S consists of lowercase English letters.

输入

Input is given from Standard Input in the following format:

N
S

输出

Print the final number of slimes.

样例输入

10
aabbbbaaca

样例输出

5

Ultimately, these slimes will fuse into abaca.

程序代码

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	string s;
	cin>>n;
	cin>>s;
	int cnt=1;
	for(int i=1;i<n;i++){
		if(s[i]!=s[i-1])
			cnt++;
	}
	cout<<cnt<<endl;
	return 0;
}

课后练习

1.栈的顺序存储和链式存储

题意

用顺序存储和链式存储方式写栈的创建、插入、删除和出栈操作。

程序代码:(顺序存储)

#include<stdio.h>
#include<stdlib.h>
#define MAX 100	//数组的最大界限 
struct SqStack{
	int top;
	int elem[MAX];
};
void InitStack(struct SqStack *p){
	p->top=0;
}
void Push(struct SqStack *p,int x){
	if(p->top<MAX-1){
		p->top=(p->top+1);
		p->elem[p->top]=x;
	}else{
		printf("\n   Overflow!");
	}
}
int Pop(struct SqStack *p){
	int x;
	if(p->top!=0){
		x=p->elem[p->top];
		p->top=p->top-1;
		return x;
	}else{
		printf("\n  Underflow!");
		return -1;
	}
}
int GetTop(struct SqStack p){
	int x;
	if(p.top!=0){
		x=p.elem[p.top];
		return x;
	}else{
		printf("\n   Underfloe!");
		return -1;
	}
}
void OutStack(struct SqStack p){
	int i,j;
	if(p.top==0){
		printf("\n  The Stack is NULL.");
	}else{
		for(i=p.top;i>0;i--){
			printf("\n  %2d %6d",i,p.elem[i]);
		}
	}
}
int main(){
	struct SqStack q;
	int x,y,cord,a;
	do{
		printf("\n");
		printf("\n      主菜单       \n");
		printf("\n     1.初始化顺序栈\n");
		printf("\n     2.插入一个元素X\n");
		printf("\n     3.删除栈顶元素\n");
		printf("\n     4.取栈顶元素\n");
		printf("\n     5.结束程序运行\n");
		printf("\n===============================\n");
		printf("     请输入您的选择(1,2,3,4,5)");
		scanf("%d",&cord);
		switch(cord){
			case 1:{
				InitStack(&q);
				OutStack(q);
				break;
			}
			case 2:{
				printf("\n a=");
				scanf("%d",&a);
				Push(&q,a);
				OutStack(q);
				break;
			}
			case 3:{
				a=Pop(&q);
				printf("\n a=%d\n",a);
				OutStack(q);
				break;
			}
			case 4:{
				y=GetTop(q);
				printf("\n  y=%d",y);
				OutStack(q);
				break;
			}
			case 5:{
				exit(0);
			}
		}
	}while(cord<=5);
	return 0;
}

程序代码:(链式存储)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct Lnode{
	int data;	//数据域 
	struct Lnode *next;	//指针域 
}Lsnode;	//结点结构类型 
void IniStack(Lsnode *top){
	top->next=NULL;
}
void Push(Lsnode *top,int x){
	Lsnode *p;
	p=(Lsnode *)malloc(sizeof(Lsnode));
	p->data=x;
	p->next=top->next;
	top->next=p;
}
int Pop(Lsnode *top){
	Lsnode *p;
	int x;
	if(top->next!=NULL){
		p=top->next;
		top->next=p->next;
		x=p->data;
		free(p);
		return x;
	}else{
		printf("Stack null!\n");
		return 0;
	}
}
void outlin(Lsnode *top){
	Lsnode *p=top->next;
	while(p){
		printf("%4d",p->data);
		p=p->next;
	}
}
int main(){
	int i,cord,x,y;
	Lsnode *top1;
	do{
		printf("\n      主菜单       \n");
		printf("     1.建立链栈\n");
		printf("     2.入栈一个元素X\n");
		printf("     3.出栈一个元素\n");
		printf("     4.结束程序运行\n");
		printf("===============================\n");
		printf("     请输入您的选择(1,2,3,4)");
		scanf("%d",&cord);
		switch(cord){
			case 1:{
				top1=(Lsnode *)malloc(sizeof(Lsnode));
				IniStack(top1);
				outlin(top1);
				break;
			}
			case 2:{
				printf("\n x=?");
				scanf("%d",&x);
				Push(top1,x);
				outlin(top1);
				break;
			}
			case 3:{
				y=Pop(top1);
				printf("\n x=%d\n",y);
				outlin(top1);
				break;
			}
			case 4:{
				exit(0);
			}
		}
	}while(cord<=4);
	return 0;
}

2.队列的顺序存储和链式存储

题意

用顺序存储和链式存储方式写队列的创建、入队、出队、取队首元素操作

程序代码:(顺序存储)

#include<stdio.h>
#include<stdlib.h>
#define MAX 1000	//数组的最大界限 
struct SeQueue{
	int front,rear;
	int elem[MAX];
};
struct SeQueue Q;
void SetNULL(struct SeQueue *Q){
	Q->front=-1;
	Q->rear=-1;
}
int Empty(struct SeQueue Q){
	if(Q.rear==Q.front)
		return 1;
	else
		return 0;
}
void AddQ(struct SeQueue *Q,int x){
	if((Q->rear+1)%MAX==Q->front){
		printf("Queue is FULL!\n");
	}else{
		Q->rear=(Q->rear+1)%MAX;
		Q->elem[Q->rear]=x;
	}
}
int DelQ(struct SeQueue *Q){
	if(Q->rear==Q->front){
		printf("Queue is Empty!\n");
		return -1;
	}else{
		Q->front=(Q->front+1)%MAX;
		return (Q->elem[Q->front]);
	}
}
int Front(struct SeQueue *Q){
	int x;
	if(Q->rear==Q->front){
		printf("Queue is Empty!\n");
		return -1;
	}else{
		x=Q->elem[(Q->front+1)%MAX];
		return x;
	}
}
void OutQueue(struct SeQueue Q){
	int i,j;
	if(Empty(Q)){
		printf("\n  队列是空的.");
	}else{
		for(i=Q.front+1;i<=Q.rear;i++){
			printf("\n %6d",Q.elem[i]);
		}
	}
}
int main(){
	struct SeQueue q;
	int i,y,cord,a;
	do{
		printf("\n");
		printf("\n      主菜单       \n");
		printf("\n     1.初始化循环队列\n");
		printf("\n     2.入队\n");
		printf("\n     3.出队\n");
		printf("\n     4.取队首元素\n");
		printf("\n     5.结束程序运行\n");
		printf("\n===============================\n");
		printf("     请输入您的选择(1,2,3,4,5)");
		scanf("%d",&cord);
		switch(cord){
			case 1:{
				SetNULL(&Q);
				break;
			}
			case 2:{
				printf("\n a=");
				scanf("%d",&a);
				AddQ(&Q,a);
				OutQueue(Q);
				break;
			}
			case 3:{
				a=DelQ(&Q);
				printf("\n a=%d\n",a);
				OutQueue(Q);
				break;
			}
			case 4:{
				y=Front(&Q);
				printf("\n  y=%d",y);
				OutQueue(Q);
				break;
			}
			case 5:{
				exit(0);
			}
		}
	}while(cord<=5);
	return 0;
}

程序代码:(链式存储)

#include<stdio.h>
#include<stdlib.h>
typedef struct NodeType{
	int data;
	struct NodeType *next;
}NodeType;
typedef struct{
	NodeType *front,*rear;
}LinkQueue;
NodeType *p,*s,*h;
void outlin(LinkQueue qq){
	p=qq.front->next;
	while(p){
		printf("data=%4d\n",p->data);
		p=p->next;
	}
	printf("\n  outend  \n");
}
void insert(LinkQueue *qe,int x){
	s=(NodeType *)malloc(sizeof(NodeType));
	s->data=x;
	s->next=NULL;
	qe->rear->next=s;
	qe->rear=s;
}
int delete(LinkQueue *qe){
	int x;
	if(qe->front==qe->rear){
		printf("队列为空。\n");
		x=0;
	}else{
		p=qe->front->next;
		qe->front->next=p->next;
		if(p->next==NULL){
			qe->rear=qe->front;
		}
		x=p->data;
		free(p);
	}
	return x;
}
void creat(LinkQueue *qe){
	int i,n,x;
	h=(NodeType *)malloc(sizeof(NodeType));
	h->next=NULL;
	qe->front=h;
	qe->rear=h;
	printf("n=");
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		printf("\n data=");
		scanf("%d",&x);
		insert(qe,x);
	}
}
int main(){
	LinkQueue que;
	int i,x,y,cord;
	do{
		printf("\n      主菜单       \n");
		printf("     1.建立链表队列\n");
		printf("     2.入队一个元素x\n");
		printf("     3.出队一个元素\n");
		printf("     4.结束程序运行\n");
		printf("===============================\n");
		printf("     请输入您的选择(1,2,3,4)");
		scanf("%d",&cord);
		switch(cord){
			case 1:{
				creat(&que);
				outlin(que);
				break;
			}
			case 2:{
				printf("\n x=?");
				scanf("%d",&x);
				insert(&que,x);
				outlin(que);
				break;
			}
			case 3:{
				y=delete(&que);
				printf("\n x=%d\n",y);
				outlin(que);
				break;
			}
			case 4:{
				exit(0);
			}
		}
	}while(cord<4);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值