三次作业

中缀变后缀

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <map>
using namespace std;
int n;
stack <char>q;
map <char,int>m;
char s[105];
void c(){
    cout<<q.top();
    q.pop();
}
void init(){
    cin>>s;
    m['(']=-1;
    m['+']=0;
    m['-']=0;
    m['*']=1;
    m['/']=1;
    m[')']=2;
    n=strlen(s);
}
void solve(){
    for(int i=0;i<n;i++){
        if(s[i]<='9'&&s[i]>='0')
           cout<<s[i];
        else{
            if(q.empty()||s[i]=='(')
               q.push(s[i]);
            else if(s[i]==')'){
               while(m[s[i]]-m[q.top()]!=3)
                    c();
                    q.pop();
            }
            else if(m[s[i]]>m[q.top()])
                q.push(s[i]);
            else if(m[s[i]]<=m[q.top()]){
                while(!q.empty()&&m[s[i]]<=m[q.top()])
                     c();
                     q.push(s[i]);
            }
        }
    }
    if(!q.empty()) c();
}

int main()
{
   init();
   solve();
}

骑士游历

​
#include<iostream>
using namespace std;
int X=8,Y=8;
int dir[8]={{-2,1},{-2,-1},{2,1},{2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
int chess[10][10];
//骑士的一步	
int next(int *x,int *y,int count){
    if(*x+dir[count][0]<X&&*y+dir[count][1]>=0&&!chess[*x+dir[count][0]][*y+dir[count][1]]){
		x+=dir[count][0];
		y+=dir[count][1];
		return 1;
	}
	return 0;
}
int travel(int x,int y,int step){
	int x1=x;
	int y1=y;
	int flag=0,count=0;
	chess[x1][y1]=step++;
	//结束条件
	if(step==X*Y) {
	for(i=0;i<X;i++){
		for(j=0;j<Y;j++)
			cout<<chess[i][j]<<" ";
		cout<<endl;
	}
	cout<<endl;
	return 1;
	}
	flag=next(&x1,&y1,count);
	while (flag==0&&count<7){
		count++;
		flag=nextxy(&x1,&y1,count);
	}
	while(flag){
		if(travel(x1,y1,step+1) return 1;
		x1=x;
		y1=y;
        count++;
		flag=next(&x1,&y1,count);
		while(flag==0)&&count<7){
			count++;
			flag=next(&x1,&y1,count);
		}
	}
	if(flag==0) chess[x][y]=0;
	return 0;
}	
int main(){
	int i,j;
	int x,y;
	if(!travel(x,y,1);
	cout<<"knight says"sorry,I can't!""<<endl;
	for(i=0;i<X;i++){
		for(j=0;j<Y;j++)
			cout<<chess[i][j]<<" ";
		cout<<endl;
	}

}

​

 kmp完整版

#include <iostream>
#include <cstring>
using namespace std;
char a[105],b[1005];
int n,m;
int next[105];
void getnext(){
     next[0]=-1;
     next[1]=0;
     int k;
   for(int i=2;a[i]!='\0';i++){
       k=next[i-1];
       while(a[k]!=a[i-1]&&k!=-1) 
		   k=next[k];
	   if(k==-1) {
		   next[i]=0;
		   continue;
	   }
       if(a[i-1]==a[k])
          next[i]=k+1;
       else
          next[i]=0;
   }
}
int main()
{
  int i=0,j=0;
      cin>>a;
      getnext();
	  n=strlen(a);
	  while(i<n) cout<<next[i++]<<" ";
      cin>>b;
      m=strlen(b);
	  i=0;
          while(j<m){
            while(a[j]==b[i]&&i<n){
               i++;
               j++;
            }
            if(j==n){
                cout<<"victory"<<endl;
                break;
            }
            if(i>=m){
                cout<<"defeat"<<endl;
                break;
            }
            j=next[j];
          }
}

递归求next数组

void getnext(){
     next[0]=-1;
     next[1]=0;
     int k;
   for(int i=2;a[i]!='\0';i++){
       k=next[i-1];
       while(a[k]!=a[i-1]&&k!=-1) 
		   k=next[k];
	   if(k==-1) {
		   next[i]=0;
		   continue;
	   }
       if(a[i-1]==a[k])
          next[i]=k+1;
       else
          next[i]=0;
   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值