您的位置:首页 > 其它

百度一道笔试题

2016-07-04 20:24 281 查看
[编程题] 单词接龙

拉姆刚开始学习英文单词,对单词排序很感兴趣。
如果给拉姆一组单词,他能够迅速确定是否可以将这些单词排列在一个列表中,使得该列表中任何单词的首字母与前一单词的为字母相同。
你能编写一个程序来帮助拉姆进行判断吗?

/*
* main.cpp
*
* Created on: 2016年6月28日
* Author: IVAN
*/

#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

int main()
{
int num;
vector<string> wolds;

while(cin >> num)
{
if(num < 2 || num > 200)
{
cout << "No" << endl;
}

wolds.clear();
string str;
while(num)
{
str.clear();
cin >> str;
wolds.push_back(str);
--num;
}

size_t i = 0;
while(i < wolds.size() - 1)
{
size_t len = wolds.at(i).size() - 1;
if(wolds.at(i).at(len) == wolds.at(i + 1).at(0))
{
++i;
}
else
{
cout << "No" << endl;
break;
}

}

// for(vector<string>::iterator iter = wolds.begin(); iter != wolds.end() - 1; ++iter)
// {
// size_t len = wolds.at(i).size() - 1;
// if((*iter).at(len) == (*(iter + 1)).at(0))
// {
// ++i;
// }
// else
// {
// cout << "NO" << endl;
// }
// }
if(i == wolds.size() - 1)
{
cout << "Yes" << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: