很多东西都在变,只有我还是当年那个不会C++只会C的笨蛋
魔王的语言
我的思路:局部栈,整体队列。
1队列:整个字符串处理采用队列。先进先出,逐个处理。
2栈:遇到“()"创建栈,先进后出,实现“倒过来”。处理之后继续队列处理。
第一遍
#include <stdio.h>
#include <stdlib.h>
//魔王语言
typedef struct QNode {
char a;
struct QNode* next;
}QNode;
//入队
QNode* enQueue(QNode* rear, char x) {
QNode* enElem = (QNode*)malloc(sizeof(QNode));
enElem->a = x;
enElem->next = NULL;//1、用节点包裹入队元素
rear->next = enElem;//2、新节点与rear节点建立逻辑关系
rear = enElem;//3、rear指向新节点
return rear;//返回新的rear,为后续新元素入队做准备
}
//出队
char DeQueue(QNode* top) {
if (top->next == NULL)
return '0';
QNode* p = top->next;
top->next = p->next;
char a = p->a;
free(p);
return a;
}
//入栈
QNode* push(QNode* stack, char x)
{
QNode* line = (QNode*)malloc(sizeof(QNode));
line->a=x;
line->next = stack;//新节点
stack = line;//更新
return stack;
}
//出栈
char pop(QNode* stack)
{
char a;
if (stack->next != NULL&&stack) {
QNode* p = stack;
stack = stack->next;
a = p->a;
free(p);
return a;
}
else
return '0';
}
int main()
{
char a,b;
QNode* queue = (QNode*)malloc(sizeof(QNode));
queue->next = NULL;
QNode* q = queue,*top=queue;
while (1)
{
a=getchar();
if( a>='a'&&a<='z' || a == 'A' || a =='B'||a=='('||a==')')
q = enQueue(q, a);
else break;
}
while (top)
{ //正常入队列
a = DeQueue(top);
if (a >= 'a' && a <= 'z')
printf("%c",a);//人类语言
else if (a == 'B')
printf("tsaedsae");
else if (a == 'A')
printf("sae");
else //stack a-->'('
{
b= DeQueue(top);//更新
QNode*stack= (QNode*)malloc(sizeof(QNode));
while (a != ')')
{
a= DeQueue(top);
stack = push(stack, a);
}
pop(stack); //pop(stack);//弹出”)
while (1)
{
if (b >= 'a' && b <= 'z')
printf("%c", b);//人类语言
else if (b == 'B')
printf("tsaedsae");
else //if (b == 'A')
printf("sae");
a = pop(stack);
if (a >= 'a' && a <= 'z')
printf("%c", a);//人类语言
else if (a == 'B')
printf("tsaedsae");
else if (a == 'A')
printf("sae");
else //if (a == '0')
break;
}
}
}
getchar();
return 1;
}
结果错误,用好几个printf检查出来,应该是因为pop函数返回值不是stack,没有把改变的结果返回回去,导致存放栈中的从来没有被删除成功过。
第二遍
#include <stdio.h>
#include <stdlib.h>
//魔王语言
typedef struct QNode {
char a;
struct QNode* next;
}QNode;
//入队
QNode* enQueue(QNode* rear, char x) {
QNode* enElem = (QNode*)malloc(sizeof(QNode));
enElem->a = x;
enElem->next = NULL;//1、用节点包裹入队元素
rear->next = enElem;//2、新节点与rear节点建立逻辑关系
rear = enElem;//3、rear指向新节点
return rear;//返回新的rear,为后续新元素入队做准备
}
//出队
char DeQueue(QNode* top) {
if (top->next == NULL)
return '0';
QNode* p = top->next;
top->next = p->next;
char a = p->a;
free(p);
return a;
}
//入栈
QNode* push(QNode* stack, char x)
{
QNode* line = (QNode*)malloc(sizeof(QNode));
line->a=x;
line->next = stack;//新节点
stack = line;//更新
return stack;
}
//出栈
QNode* pop(QNode* stack)
{
if (stack->next != NULL&&stack) {
QNode* p = stack;
stack = stack->next;
free(p);
}
return stack;
}
int main()
{
char a,b;
QNode* queue = (QNode*)malloc(sizeof(QNode));
queue->next = NULL;
QNode* q = queue,*top=queue;
while (1)
{
a=getchar();
if( a>='a'&&a<='z' || a == 'A' || a =='B'||a=='('||a==')')
q = enQueue(q, a);
else break;
}
while (top)
{ //正常入队列
a = DeQueue(top);
if (a >= 'a' && a <= 'z')
printf("%c",a);//人类语言
else if (a == 'B')
printf("tsaedsae");
else if (a == 'A')
printf("sae");
else //stack a-->'('
{
b= DeQueue(top);//更新
QNode*stack= (QNode*)malloc(sizeof(QNode));
while (a != ')')
{
a= DeQueue(top);
stack = push(stack, a);
}
//stack=pop(stack); //弹出”)
while (1)
{
if (b >= 'a' && b <= 'z')
printf("%c", b);//人类语言
else if (b == 'B')
printf("tsaedsae");
else //if (b == 'A')
printf("sae");
if (stack->next != NULL)
a = stack->next->a;
else
break;
stack = pop(stack);
if (a >= 'a' && a <= 'z')
printf("%c", a);//人类语言
else if (a == 'B')
printf("tsaedsae");
else if (a == 'A')
printf("sae");
else //if (a == '0')
break;
}
} //其实stack和queue的定义是一样的,只是操作不一样。
}
return 1;
}
本地测试没有问题但是结果判定为超时(耗时1006ms)。
问题:效率低,应该先(倒着存放进去)存放在栈当中,局部使用队列。
第三遍:数组
数组就是万能的,吹爆数组。
#include <stdio.h>
void stack(int i, int k, char word[])
{//思路:递归解决多层嵌套问题
//功能:打印一对儿括号里面的内容
//传入参数:i,k;第一个和')'的位置,word;字符串
//i:用来打印第一位
int j, begin;
if (i == k )return;//空括号
else
{//第一位
if (word[i] == 'B')
printf("tsaedsae");
else if (word[i] == 'A')
printf("sae");
else printf("%c", word[i]);//因为没有考虑(())情况
}
for (j = k - 1;j > i; j--)
{
if (word[j] == '(') continue;
if (word[j] == ')')
{
for (begin = j; word[begin] != '('; begin--) ;
stack(begin+1, j, word);
j = begin;
}
else
{
if (word[j] == 'B')
printf("tsaedsae");
else if (word[j] == 'A')
printf("sae");
else printf("%c", word[j]);
}
if (word[i] == 'B')
printf("tsaedsae");
else if (word[i] == 'A')
printf("sae");
else printf("%c", word[i]);//因为没有考虑(())情况
}
}
int main()
{
char word[1000] = { 0 }; scanf_s("%s", word,1000);
int i=0, begin=0,k;
while (1)
{
if (word[i] == 'B')
printf("tsaedsae");
else if (word[i] == 'A')
printf("sae");
else if (word[i] == ')')
;
else if (word[i] == '(')
{
begin = i + 1; i++;
for (k = 1; k ; i++)
{//找到end
if (word[i] == '(')
k++;//k标记括号的数目
else if (word[i] == ')')
k--;
if (k <= 0)// once k=0, break instant
break;
}
stack(begin,i, word);
}
else//(word[i] >= 'a' && word[i] <= 'z')
printf("%c", word[i]);
i++;
if(word[i]==0) break;//终止
}
return 0;
}
题上明明没有说有CDEF情况,我咋能想到还需要考虑别的大写字母呢,改上这一点之后就好了
补充:
涉及连续嵌套的时候,不同的理解会造成不同的结果。
比如: (RMRGRJRDRORsaeRGRDFGA)=RsaeRGRFRDRRRGRRReRaRsRRRORRRDRRRJRRRGRRRMR
还是((RGAODJGM)DFGA) =RsaeRGRFRDRRRGRRRsaeRRRORRRDRRRJRRRGRRRMR
再见!魔王!