判断一个序列是否是另一个序列的前缀

// test13.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> longNum;
	vector<int> shortNum;
	int num,flag=1;
	cout << "请输入长序列:" << endl;
	while (cin >> num)
	{
		longNum.push_back(num);
		if (getchar() == '\n')
			break;
	}

	cout << "请输入短序列:" << endl;
	while (cin >> num)
	{
		shortNum.push_back(num);
		if (getchar() == '\n')
			break;
	}

	

	for (auto it = longNum.begin(); it != longNum.end(); it++)
	{
		cout << *it << " ";
	}cout << endl;

	for (auto it = shortNum.begin(); it != shortNum.end(); it++)
	{
		cout << *it << " " ;
	}cout << endl;


	for (int i = 0; i < shortNum.size(); i++)
	{
		if (shortNum[i] != longNum[i])
		{
			flag = 0;
			break;
		}
	}
	if (flag == 1)
		cout << "短序列是长序列的前缀" << endl;
	else
		cout << "短序列不是长序列的前缀" << endl;

}
posted @ 2016-09-27 22:33  wdan2016  阅读(344)  评论(0编辑  收藏  举报