百度一道笔试题
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;
}
拉姆刚开始学习英文单词,对单词排序很感兴趣。
如果给拉姆一组单词,他能够迅速确定是否可以将这些单词排列在一个列表中,使得该列表中任何单词的首字母与前一单词的为字母相同。
你能编写一个程序来帮助拉姆进行判断吗?
/*
* 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;
}
相关文章推荐
- 秦晖:王小波是一个自由主义思想家吗?
- RTSP协议介绍
- php5中this关键字用法讲解
- 记搜狗机器学习实习生面试
- java.lang.NoClassDefFoundError: org/springframework/beans/factory/support/DefaultListableBeanFactory
- 2.1 nagios 安装
- java框架之spring基础应用
- W3AF FuzzableRequest 处理流程札记
- 剑指offer---二叉树的镜像---java实现
- 华为5700交换机通过外部开源protal和本地aaa用户认证的一些问题
- kcachegrind gui for callgrind
- 7z制作自解压安装包
- 华为5700交换机通过外部开源protal和本地aaa用户认证的一些问题
- HTTP请求报文和HTTP响应报文
- c# 16进制显示转化
- VS2010/MFC编程(对话框:模态对话框及其弹出过程)
- RecyclerView之垂直ListView
- iOS滑动返回的最简单实现
- Lucene 学习(一):简单demo
- pandas入门(持续更新)