题目1 : 逆序单词
时间限制: 10000ms
单点时限:
1000ms
内存限制:
256MB
描述
在英文中有很多逆序的单词,比如dog和god,evil和live等等。
现在给出一份包含N个单词的单词表,其中每个单词只出现一次,请你找出其中有多少对逆序单词。
输入
第1行:1个整数,N,表示单词数量。2≤N≤50,000。
第2..N+1行:每行1个单词,只包含小写字母,每个单词长度不超过16个字母。保证每个单词只出现一次,且不会出现回文单词(即一个单词倒序还是它自己,比如eye)。
输出
第1行:1个整数,表示单词表中逆序单词的对数。
6 dog live hiho evil coder god样例输出
2
题目分析:
这题我们第一想法是取出每一个单词,逆序后在原单词集合中查找,若是找到,说明存在这么一对逆序相等的单词,用一个临时变量temp记录一下,遍历完原集合的所有集合,这一题也就解决了。
OK,有了大致的思路,我们就可以开始了,编程能力像博主一样一般般的可以先把自己的想法用代码实现,至于会不会超时,会不会占用较大内存,先暂时不予考虑。我们基本的想法是用数组来实现。
代码实现:
#include <iostream>
#include <algorithm>
#define maxnum 50000
using namespace std;
int n; //单词个数
string a[maxnum]; //定义字符串数组
string b[maxnum]; //用来接收逆序的字符串数组
int temp = 0; //临时变量,用来记录数量
int calcul(string b[], string a[], int n)
{
int k = 0;
while(k < n) //循环用来控制从数组第一个元素到最后一个元素
{
b[k] = a[k];
reverse(b[k].begin(), b[k].end()); //逆序单词
for(int j = 0; j < n; j++)
{
if(b[k].size() != a[j].size()) //这是为了减少循环的次数
continue;
if( *(b[k].begin()) != *(a[j].begin()) ) //减少循环的次数
continue;
if(b[k] == a[j])
{
temp += 1;
}
}
k++;
}
return temp/2;
}
int main()
{
cin >> n;
int i = 0;
while(i < n)
{
cin >> a[i];
b[i] = a[i];
reverse(b[i].begin(), b[i].end());
i++;
}
//cout << "---------------------------------" << endl;
cout << calcul(b,a,n) << endl;
/*
cout << "-----------------------------------------" << endl;
for(int i = 0; i < n; i++)
{
cout << a[i] << endl;
}
cout << "--------------------------------------------" << endl;
for(int i = 0; i < n; i++)
{
cout << b[i] << endl;
}
*/
return 0;
}
提交之后显示
Time Limit Exceeded | TLE | 用户程序运行时间超过题目的限制 |
我们简单分析代码则可以直观的看到,使用了太多的循环,导致程序执行过慢。我们想办法减少局部循环的层数,发现效果也并不明显。那我们很容易想到更换工具,使用与数组相似的工具--set,前面的博客我们介绍了set的基本用法,set里面内置一个红黑树,内部索引自动排序,检索起来会容易的多。
我们对代码进行修改优化:
#include <iostream>
#include <set>
#include <algorithm>
#define maxnum 50000
using namespace std;
int n; //单词个数
string str; //定义一个字符串类型的变量,用来接收单个单词
//string a[maxnum];
//string b[maxnum];
int temp = 0; //临时变量,用来记录数量
/*
void rev(string b[], int n)
{
for(int i = 0; i < n; i++)
{
reverse(b[i].begin(), b[i].end());
}
}
*/
/*
int calcul(string b[], string a[], int n)
{
int k = 0;
while(k < n)
{
b[k] = a[k];
reverse(b[k].begin(), b[k].end());
for(int j = 0; j < n; j++)
{
if(b[k].size() != a[j].size())
continue;
if( *(b[k].begin()) != *(a[j].begin()) )
continue;
if(b[k] == a[j])
{
temp += 1;
}
}
k++;
}
/*
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(b[i].size() != a[j].size())
continue;
if(b[i] == a[j])
{
temp += 1;
}
}
}
return temp/2;
}
*/
int main()
{
set<string> s;
cin >> n;
int i = 0;
while(i < n)
{
cin >> str;
s.insert(str); //赋值
//b[i] = a[i];
//reverse(b[i].begin(), b[i].end());
i++;
}
//cout << "---------------------------------" << endl;
set<string> :: iterator itor = s.begin(); //定义迭代器
while(itor != s.end())
{
str = *itor; //依次取出迭代器所指的单词
reverse(str.begin(), str.end()); //将单词逆序,还保存在str中
//cout << str << endl;
if(s.find(str) != s.end()) //寻找有无相同的
temp++; //找到就计数
//cout << *itor << endl;
itor ++; //迭代器迭代
}
cout << temp/2 <<endl;
//rev(b,n);
//cout << calcul(b,a,n) << endl;
/*
cout << "-----------------------------------------" << endl;
for(int i = 0; i < n; i++)
{
cout << a[i] << endl;
}
cout << "--------------------------------------------" << endl;
for(int i = 0; i < n; i++)
{
cout << b[i] << endl;
}
*/
//cout << "a[0].size() : " << a[0].size() << endl;
//cout << *(a[0].begin()) << endl;
return 0;
}