华为OJ算法题(一)

1、火车进站

class train_station
{
public:
	void get_result()  
    {  
        int num=0;  
        int tem=0;  
        cin>>num;  
        int size=num; 
        while(size--)  
        {  
            cin>>tem;  
            numbers.push_back(tem);   ///保存进站顺序  
        }  
        _partion(numbers,numbers.begin());  //排列组合  
        sort(re.begin(),re.end());  
        //找出符合要求的  
        for(size_t i=0;i<re.size();i++)  
        {  
            if(_is_true(re[i],numbers,numbers.size()))  
            {  
                for(size_t j=0;j<re[i].size();j++)  
                {  
                    cout<<re[i][j];  
                    if((j<re[i].size()-1))  
                        cout<<" ";  
                }  
				cout<<endl; 
            }
        }  
    }  
private:
	void  _partion(vector<int> &str,vector<int>::iterator begin)   //排列组合  注意必须是向量的引用,不然会出现迭代器失效的问题
	{
		if(begin==str.end())
		{
			re.push_back(str);
		}
		else
		{
			for(vector<int>::iterator i=begin;i!=str.end();i++)
			{
				int tem=*i;
				*i=*begin;
				*begin=tem;

				 _partion(str,begin+1);

				 tem=*i;
				*i=*begin;
				*begin=tem;
			}
		}
	}
	bool _is_true(vector<int> &popstr,vector<int> &pushstr,int length)   //判断该组合是否是火车的正确出栈顺序
	{
		//numbers是入站序列,str是出站序列
		if(popstr.empty()||pushstr.empty()||length<=0)  
            return false;  
		vector<int>::iterator popOrder=popstr.begin();
		vector<int>::iterator pushOrder=pushstr.begin();
        int i=0,j=0;
        for(;;)  
        {  
            if(!s1.empty()&&j<length&&popstr[j]==s1.top()) //防止地址访问出界  
            {  
                j++;  
                s1.pop();  
            }  
            if(s1.empty()||(popstr[j]!=s1.top()))  
            {  
                if(i>=length||j>=length)
                    break;  
                s1.push(pushstr[i]);  
                i++;  
            }                     
        }
		while(!s1.empty()) //把栈清空
		{
			s1.pop();
		}
        if(i==length&&j==length)  
            return true;
        else  
            return false;
	}
private:
	vector<int> numbers;  //保存输入数据
	vector<vector<int>> re;  //保存所有的排列组合
	stack<int> s1;
};

2、比较两手牌的大小

class comp
{
public:
	void start()
	{
		string str;
		vector<char> one;
		vector<char> two;

		while(getline(cin,str))
		{
			size_t i=0;
			while(str[i]!='-')
			{
				if(str[i]!=' ')
					one.push_back(str[i]);
				i++;
			}
			i++;
			while(i<str.size())
			{
				if(str[i]!=' ')
					two.push_back(str[i]);
				i++;
			}

			//判断哪个序列大
			vector<char> result=_large_str(one,two);

			//输出
			if(have_joker(result)&&result.size()==10)
			{
				if(result[1]=='o')
					cout<<"joker JOKER"<<endl;
				else 
					cout<<"JOKER joker"<<endl;
			}
			else if(have_joker(result)&&result.size()==5)
			{
				if(result[1]=='o')
					cout<<"joker"<<endl;
				else 
					cout<<"JOKER"<<endl;
			}
			else
			{
				for(size_t i=0;i<result.size();i++)
				{
					if(result[i]=='1')
					{
						cout<<result[i];
						continue;
					}
					if(i<result.size()-1)
						cout<<result[i]<<" ";
					else
						cout<<result[i];
				}
				cout<<endl;
			}

			//清空向量
			result.clear();
			one.clear();
			two.clear();
		}

	}
private:
	vector<char> _large_str(vector<char> one,vector<char> two)
	{
		if(have_joker(one)&&one.size()==10)  //有王炸直接输出王炸的向量
			return one;
		else if(have_joker(two)&&two.size()==10) //有王炸直接输出王炸的向量
			return two;
		else if(have_joker(one)&&two.size()==1) //只有一个王的比较
			return one;
		else if(have_joker(two)&&one.size()==1)  //只有一个王的比较
			return two;
		else if(have_joker(one)&&have_joker(two))  //两个大小王比较
		{
			if(one[0]=='J')
				return one;
			else 
				return two;
		}

		//如果没有王,则正常比较
		if(!have_joker(one)&&!have_joker(two))
		{
			//都没有王时,炸弹最大
			if(one.size()==4&&two.size()!=4)
			{
				//考虑4个10的情况
				if(two.size()==8)
				{
					if(get_value(one[0])>10)
						return one;
					else
						return two;
				}
				return one;
			}
			else if(one.size()!=4&&two.size()==4)
			{
				//考虑4个10的情况
				if(one.size()==8)
				{
					if(get_value(two[0])>10)
						return two;
					else
						return one;
				}
				return two;
			}
			else
			{
				int one_value=get_value(one[0]);
				int two_value=get_value(two[0]);
				if(one_value>two_value)
					return one;
				else 
					return two;
			}
		}
		return vector<char>();  //返回一个临时向量
	}
	bool have_joker(vector<char> &one)
	{
		if(one.size()>=5)
			if(one[1]=='o'||one[1]=='O')
				return true;
			else 
				return false;
		else
			return false;
	}
	int get_value(char c)
	{
		if(c=='J')
			return 11;
		else if(c=='Q')
			return 12;
		else if(c=='K')
			return 13;
		else if(c=='A')
			return 14;
		else if(c=='2')
			return 15;
		else
		{
			if(c=='1')
				return 10;
			return c-'0';
		}
	}
};

3、字符串去重

class  str_solution
{
public:
	void start()
	{
		string str;
		bool flag[128]={false};
		while(true)
		{
			cin>>str;
			for(int i=0;i<str.size();i++)
			{
				if(flag[str[i]]==true)
				{
					copy(str.begin()+i+1,str.end(),str.begin()+i);
					str[str.size()-1]='\0';
					if(str[i]!='\0')
						i--;
				}
				else
					flag[str[i]]=true;
			}
			cout<<str<<endl;
			memset(flag,false,128);
		}
	}
};

4、座标移动

class axis_move
{
public:
	axis_move()
	{
		x_start=0;
		y_start=0;
	}
	void start()
	{
		string str;
		int sum=0;
		while(getline(cin,str))
		{
			for(int i=0;i<str.size();i++)
			{
				switch(str[i])
				{
				case 'A':  //左移
					i++;
					while(str[i]!=';')
					{
						if(str[i]>='0'&&str[i]<='9')
						{
							sum=sum*10+str[i]-'0';
							i++;
						}
						else if(str[i]==' ')
						{
							i++;
						}
						else
						{
							while(str[i]!=';')
							{
								i++;
							}
							sum=0;
						}
					}
					if(sum!=0)
					{
						x_start-=sum;
						sum=0;
					}
					break;
				case 'D':  //左移
					i++;
					while(str[i]!=';')
					{
						if(str[i]>='0'&&str[i]<='9')
						{
							sum=sum*10+str[i]-'0';
							i++;
						}
						else if(str[i]==' ')
						{
							i++;
						}
						else
						{
							while(str[i]!=';')
							{
								i++;
							}
							sum=0;
						}
					}
					if(sum!=0)
					{
						x_start+=sum;
						sum=0;
					}
					break;
				case 'W':  //左移
					i++;
					while(str[i]!=';')
					{
						if(str[i]>='0'&&str[i]<='9')
						{
							sum=sum*10+str[i]-'0';
							i++;
						}
						else if(str[i]==' ')
						{
							i++;
						}
						else
						{
							while(str[i]!=';')
							{
								i++;
							}
							sum=0;
						}
					}
					if(sum!=0)
					{
						y_start+=sum;
						sum=0;
					}
					break;
				case 'S':  //左移
					i++;
					while(str[i]!=';')
					{
						if(str[i]>='0'&&str[i]<='9')
						{
							sum=sum*10+str[i]-'0';
							i++;
						}
						else if(str[i]==' ')
						{
							i++;
						}
						else
						{
							while(str[i]!=';')
							{
								i++;
							}
							sum=0;
						}
					}
					if(sum!=0)
					{
						y_start-=sum;
						sum=0;
					}
					break;
				default:
						while(str[i]!=';')
							i++;
						break;
				}
			}
			cout<<x_start<<" "<<y_start<<endl;
		}
	}
private:
	int x_start;
	int y_start;
};



点赞