目录
有些是cpp,有些是c,自己甄别。都不是裸体代码,请自便,仅供参考。
XDOJ0106 魔王语言解释
//xdoj0106.cpp
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
//自己用例: B(kab(hij)A)B
//注意规则,希腊字母可以表示大写或小写,所以括号内部遇到大写不应该直接翻译
//如上,处理括号后,应该是
//B(kab hjhih A)B
//BkAkhkikhkjkhkbkaB
int main(){
int p=0;//p用于操作原始语言
char ch;
string MonsterWord;//存储原始语言
stack<char> S;//存储 处理过括号 的语言
queue<char> Q;
stack<char> Final;
cin>>MonsterWord;
//接下来处理括号
while(p<MonsterWord.size()){
//为什么是右括号分界,考虑到括号嵌套的情况,例如B(kh(abc))B
//要先翻译(abc)
if(MonsterWord.at(p)!=')'){
//不是最内层括号的话,就先入栈S
S.push(MonsterWord.at(p));
}else if(MonsterWord.at(p)==')'){
//判断到第一个右括号,也就是最内层的括号
//开始入队列,注意入队列停止条件是第一个左括号
while(S.top()!='('){
//不是左括号,就入队
Q.push(S.top());
S.pop();//出栈
}
//等于左括号,没入队,但是把S存的左括号出一个
S.pop();
//左括号,入队完毕,开始出队,交错出队,并入栈到S
while(Q.back()!=Q.front()){//只要队列并非只剩一个,就一直交叉入队
S.push(Q.back());
S.push(Q.front());
Q.pop();//弹出第一个元素
}
//剩一个,就是所谓的theta
S.push(Q.back());
Q.pop();
}
++p;
}
//接下来从S中翻译A,B
while(!S.empty()){
Final.push(S.top());
S.pop();
}
while(!Final.empty()){
if(Final.top()=='A'){
cout<<"sae";
}else if(Final.top()=='B'){
cout<<"tsaedsae";
}else{
cout<<Final.top();
}
Final.pop();
}
return 0;
}
XDOJ0107 迷宫问题
//xodj0107.c
#include<stdio.h>
#include<stdlib.h>
//提前声明
typedef struct PosType{
int x;
int y;
}PosType;//坐标
typedef struct SNode{
int step;//当前位置在路径上的序号
PosType seat;//当前的坐标位置
int d;//direction 往下一坐标的方向
struct SNode* next;
}SNode,*Stack;
//全局变量
Stack S;//S始终指向栈顶,且带头结点
int Map[100][100];//0表示可以走,1表示障碍物,2表示曾经走过
void InitMap(int m,int n){
for(int i=0;i<=m+1;i++){
for(int j=0;j<=n+1;j++){
if(i==0||j==0||i==m+1||j==n+1){
Map[i][j]=1;//1表示障碍物
}
}
}
}
//栈的相关操作
Stack InitStack(){
Stack top=(Stack)malloc(sizeof(SNode));
top->next=NULL;
return top;
}
int StackEmpty(Stack top){
if(top->next == NULL){
return 1;
}else{
return 0;
}
}
//向S压入节点pos
int Push(Stack top,SNode* pos){
Stack node = (Stack)malloc(sizeof(SNode));
if(node == NULL){
return 0;
}else{
node->seat.x = pos->seat.x;
node->seat.y = pos->seat.y;
node->d = pos->d;
node->next = top->next;
top->next = node;
return 1;
}
}
//弹出S的顶部,并返回该结点
SNode* Pop(Stack top){
Stack node;
if(top->next == NULL){
return NULL;
}else{
node = top->next;
top->next = node->next;
return node;
}
}
//判断当前位置是否可以通过,是则返回1,否返回0,可通的定义是未曾走到过的通道块
int Pass(PosType pos){
//可通且没走过,才算pass
if(Map[pos.x][pos.y]==1){
return 0;//1表示障碍物,不可通
}
if(Map[pos.x][pos.y]==2){
return 0;//2表示走过的路径
}
return 1;
}
//留下足迹,即给Map赋值2,表示走过
void FootPrint(PosType pos){
Map[pos.x][pos.y]=2;
return;
}
//返回curpos当前位置的下一d方向的位置
PosType NextPos(PosType curpos,int d){
//注意题目应该是东西方向是y,南北方向是x
if(d==1){//东
curpos.y++;
}else if(d==2){
//南
curpos.x++;
}else if(d==3){
//西
curpos.y--;
}else if(d==4){
//北
curpos.x--;
}
return curpos;
}
//标记不能走的通路
void MarkPrint(PosType pos){
Map[pos.x][pos.y]=1;
return;
}
int MazePath(PosType start,PosType end){
//设定当前位置为入口
PosType curpos=start;
int curstep=1;//探索第一步
SNode* e;//e节点用于存储step,seat,d等信息
do{
if(Pass(curpos)){
//如果当前位置可以通过
FootPrint(curpos);//留下足迹
//对e结点赋值
e=(SNode*)malloc(sizeof(SNode));
e->step=curstep;
e->seat=curpos;
e->d=1;
Push(S,e);//加入路径
if(curpos.x==end.x&&curpos.y==end.y){
return 1;//找到出口
}
curpos=NextPos(curpos,1);//下一位置是当前位置的东
curstep++;//探索下一步
}else{
//当前位置不可以通过
if(!StackEmpty(S)){//栈非空
e=Pop(S);
while(e->d==4&&!StackEmpty(S)){
MarkPrint(e->seat);//留下不能通过的标记,并退回一步
e=Pop(S);
}
if(e->d<4){
e->d++;
Push(S,e);//换一个方向探索
curpos=NextPos(e->seat,e->d);//设定当前位置为新方向上的相邻块
}
}
}
}while(!StackEmpty(S));
return 0;
}
int main(){
//输入
int m,n;
PosType start;
PosType end;
S=InitStack();
scanf("%d%d",&m,&n);
InitMap(m,n);//初始化地图
scanf("%d%d",&start.x,&start.y);
scanf("%d%d",&end.x,&end.y);
//输入迷宫
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
scanf("%d",&Map[i][j]);
}
}
//1右 2下 3左 4上
//这里开始解决迷宫
if(MazePath(start,end)){
//接下来输出栈S
Stack res=InitStack();
Stack top=S->next;
while(top){
Push(res,top);
top=top->next;
}
top=res->next;
while(top->next){
printf("(%d,%d,%d),",top->seat.x,top->seat.y,top->d);
top=top->next;
}
printf("(%d,%d,%d)",top->seat.x,top->seat.y,top->d);
}else{
printf("no");
return 0;//没有通路输出no
}
return 0;
}
XDOJ0256 括号匹配问题
//xdoj0256.c
#include<stdio.h>
#include<string.h>
void IsMatch(char* str,char* stack){
int i=0,base=0,top=0;
while(str[i]!='\0'){
if(str[i]=='('||str[i]=='{'||str[i]=='['){
stack[top++]=str[i];
}else if(str[i]==')'){
if(stack[top-1]=='('){
--top;
}else{
printf("Mismatch\n");
return;//因为只用判断一次,可以直接返回0退出该函数
}
}else if(str[i]=='}'){
if(stack[top-1]=='{'){
--top;
}else{
printf("Mismatch\n");
return;
}
}else if(str[i]==']'){
if(stack[top-1]=='['){
--top;
}else{
printf("Mismatch\n");
return;
}
}
++i;
}
if(top==base){
printf("Match\n");
}else{
printf("Mismatch\n");
}
return;
}
int main(){
int i=0;
char stack[128],str[100][128];
while(1){
scanf("%s",&str[i]);
if(strcmp(str[i],"**0000**")==0){
break;
}
IsMatch(str[i],stack);
}
return 0;
}
XDOJ0265 队列中的元素排序
//xdoj0265.cpp
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
using namespace std;
int main(){
int n;
scanf("%d",&n);
vector<int> a(n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a.begin(),a.end());
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
return 0;
}
XDOJ0267 判断栈输出顺序正确与否
//xdoj0267.cpp
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int PushSq[1000];//第二行的数据
int flag[1000];//标记正确与否
int data[1000];//需要判断的数据
int main(){
int M,N,K;//栈的最大长度,压栈数据长度,测试数据数
cin>>M>>N>>K;
//压栈数据
for(int i=0;i<N;++i){
cin>>PushSq[i];
}
for(int i=0;i<K;++i){
for(int j=0;j<N;++j){
cin>>data[j];//一行一行判断,输入需要判断的数据data
}
stack<int> s;//在内部循环,无需重新清空
int p=0,j=0;//p操作PushSq j操作data
while(s.size()<=M && j<N && p<=N){//只要栈不满,p,j没越界
if(!s.empty() && s.top()==data[j]){
s.pop();
j++;//需要验证的序列 指针右移
}else{
if(s.size()<=M && p<N){
//栈未满且p未越界
s.push(PushSq[p]);
}
++p;
}
}
if(s.empty()){//如果栈空,说明全部都对的上,说明正确
flag[i]=1;
}else{//栈非空,存在错误
flag[i]=0;
}
}
for(int i=0;i<K;++i){
if(flag[i]){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
XDOJ0268 括号配对问题
//xdoj0268.c
#include<stdio.h>
int main(){
int n,top=0,base=0,i=0,j=0,flag=1;
char str[100][100],stack[100];
scanf("%d",&n);
for(int k=0;k<n;k++){
scanf("%s",&str[k]);
}
while(j<n){
while(str[j][i]!='\0'){
if(str[j][i]=='('||str[j][i]=='{'||str[j][i]=='['){
stack[top++]=str[j][i];
}else if(str[j][i]==')'){
if(stack[top-1]=='('){
--top;
}else{
printf("No\n");
flag=0;
break;
}
}else if(str[j][i]=='}'){
if(stack[top-1]=='{'){
--top;
}else{
printf("No\n");
flag=0;
break;
}
}else if(str[j][i]==']'){
if(stack[top-1]=='['){
--top;
}else{
printf("No\n");
flag=0;
break;
}
}
++i;
}
if(flag){
if(top==base){
printf("Yes\n");
}else{
printf("No\n");
}
}
++j;
top=0;
i=0;
flag=1;
}
return 0;
}
XDOJ0305 判断堆栈出栈序列是否有效
//xdoj0305.c
#include<stdio.h>
//123...n的初始栈,弹出的顺序必定部分递减
//也就是说,弹出序列中 任意第i个数往后,小于这个数的这些数,必然是递减的
//4123 不符合,第一个数是4 小于4的数123不是递减
int Judge(int n,int* nums){
int flag=0,min;//假设不符合
for(int i=0;i<n;i++){
min=nums[i]-1;
for(int j=i+1;j<n;j++){
if(min==0){
//说明第i个元素是1,那么直接跳过
break;
}
if(nums[j]>nums[i]){
//大于第i个数的忽略
continue;
}else if(nums[j] < nums[i]){
//小于第i个数的数,如果不是递减,直接返回不符合
if(nums[j]<=min){
min=nums[j];//刷新min
}else{
return flag;
}
}else if(nums[j]==nums[i]){
return flag;//绝对不可能的情况
}
}
}
flag=1;
return flag;
}
int main(){
int n,nums[100];
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&nums[i]);
}
if(Judge(n,nums)){
printf("yes");
}else{
printf("no");
}
return 0;
}
XDOJ0307 括号匹配的检验
//xdoj0307.c
#include<stdio.h>
int main(){
int top=0,base=0,i=0;
char str[100],stack[100],ch;
//傻逼oj不能用gets
while(scanf("%c", &ch) && ch != '\n'){
str[i++] = ch;
}
str[i] = '\0';
i=0;
while(str[i]!='\0'){
if(str[i]=='('||str[i]=='{'||str[i]=='['){
stack[top++]=str[i];
}else if(str[i]==')'){
if(stack[top-1]=='('){
--top;
}else{
printf("wrong");
return 0;//因为只用判断一次,可以直接返回0退出主程序
}
}else if(str[i]=='}'){
if(stack[top-1]=='{'){
--top;
}else{
printf("wrong");
return 0;
}
}else if(str[i]==']'){
if(stack[top-1]=='['){
--top;
}else{
printf("wrong");
return 0;
}
}
++i;
}
if(top==base){
printf("right");
}else{
printf("wrong");
}
return 0;
}
最后
感兴趣的可以关注我的微信公众号,第一时间收到动态