http://challenge.greplin.com/上的3道题。

2024-03-06 12:58

本文主要是介绍http://challenge.greplin.com/上的3道题。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近写代码比较少,苦于没机会练手,就去找了一些编程网站,做几道题,昨晚发现http://challenge.greplin.com/有3道题,时间要求20分钟到3个小时,题目不难,把它完成了,贴出代码。


第一题,找出一长串字符串和其逆序串中,最长匹配字串。

#include "MoonString.h"
MoonString::MoonString(void)
{
}
MoonString::~MoonString(void)
{
}
string MoonString::Reverse(const string &srcString)
{
size_t len = srcString.length();
string outString;
for(size_t i = 0; i < len; i++)
{
outString += srcString[len - i - 1];
}
return outString;
}


#pragma once
#include <string>
using namespace std;
class MoonString
{
public:
MoonString(void);
~MoonString(void);
//************************************
// Method:    Reverse
// Access:    public static 
// Describe:  字符串逆序
// Parameter: const string & srcString  要逆序的字符串
// Returns:   std::string               逆序结果
//************************************
static string Reverse(const string &srcString);
};

/*
The Cue Programming Challenge
Level 1
----------------------------------------
Embedded in this block of text is the password for level 2.
The password is the longest substring that is the same in reverse.
As an example, if the input was "I like racecars that go fast"
the password would be "racecar".
Enter the password to access level 2:
*/
/*FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth*/
#include <iostream>
#include <windows.h>
#include <ctime>
#include <assert.h>
#include <string>
#include "MoonString.h"
using namespace std;
//************************************
// Method:    GetMaxSubString
// Access:    public
// Describe:  查找2个字符串中重叠的最长字符串
// Parameter: const string & str1
// Parameter: const string & str2
// Returns:   std::string
//************************************
string GetMaxSubString(const string &str1, const string &str2)
{
size_t len = min(str1.length(), str2.length());
size_t currLen = 0;
size_t maxLen = 0;
size_t maxSubStrStartIndex = 0;
for(size_t i = 0 ; i < len; i++)
{
if(str1[i] == str2[i])
{
currLen++;
if(currLen > maxLen)
{
maxLen = currLen;
maxSubStrStartIndex = i - currLen + 1;
}
}
else
{
currLen = 0;
}
}
return str1.substr(maxSubStrStartIndex, maxLen);
}
//************************************
// Method:    GetMaxReverseSubString
// Access:    public 
// Describe:  字符串2固定,字符串1逐渐缩短,找到相同索引最长的字串
// Parameter: const string & str1
// Parameter: const string & str2
// Returns:   std::string
//************************************
string GetMaxReverseSubString(const string &str1, const string &str2)
{
size_t len = str1.length();
size_t maxLen = 0;
size_t currLen = 0;
string currSubStr;
string maxSubStr;
for(size_t i = 0; i < len; i++)
{
currSubStr = GetMaxSubString(str1.substr(i), str2);
currLen = currSubStr.length();
if(currLen > maxLen)
{
maxLen = currLen;
maxSubStr = currSubStr;
}
}
return maxSubStr;
}
void F1()
{
cout << "void F1()" << endl;
LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);
/*********************************算法开始*******************************/
//const string INPUT_TEXT = "123456789987654321";
const string INPUT_TEXT = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";
string reverseStr = MoonString::Reverse(INPUT_TEXT);
string maxSubStr1 = GetMaxReverseSubString(INPUT_TEXT, reverseStr);
string maxSubStr2 = GetMaxReverseSubString(reverseStr, INPUT_TEXT);
string maxSubStr = maxSubStr1.length() > maxSubStr2.length() ? maxSubStr1 : maxSubStr2;
cout << "最长子串为:" << endl
<< maxSubStr << endl
<< "长度为:" << maxSubStr.length() << endl;
/*********************************算法结束*******************************/
QueryPerformanceCounter(&timeEnd);
cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;
time_t currentTime = time(NULL);
const char BEEP_CHAR = '\007';
const int MAX_TIME_CHAR = 30;
char timeStr[MAX_TIME_CHAR];
ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime);
cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR;
system("pause");
}
//主函数
int main()
{
F1();
return 0;
}
/*
void F1()
最长子串为:
ranynar
长度为:7
Total Milliseconds is 182.737
By GodMoon
Fri Mar 08 21:41:24 2013
*/


第二题,分为几部分,第一部车是找出大于227000的最小素数同时也是Fibonacci数X,将其加1得Y,求Y的所有唯一质因数之和。

#pragma once
#include <windows.h>
class MoonMath
{
public:
MoonMath(void);
~MoonMath(void);
//************************************
// Method:    IsInt
// Access:    public 
// Describe:  判断double值在epsilon的范围内是否很接近整数
//            如1.00005在epsilon为0.00005以上就很接近整数
// Parameter: double doubleValue    要判断的double值
// Parameter: double epsilon        判断的精度,0 < epsilon < 0.5
// Parameter: INT32 & intValue      如果接近,返回最接近的整数值
// Returns:   bool                  接近返回true,否则返回false
//************************************
static bool IsInt(double doubleValue,double epsilon,INT32 &intValue);
//************************************
// Method:    Sign
// Access:    public 
// Describe:  获取value的符号
// Parameter: T value   要获取符号的值
// Returns:   INT32     正数、0和负数分别返回1、0和-1
//************************************
template <typename T>
static INT32 Sign(T value);
//************************************
// Method:    IsPrimer
// Access:    public 
// Describe:  判断一个数是否是素数
// Parameter: UINT32 num    要判断的数
// Returns:   bool          是素数返回true,否则返回false
//************************************
static bool IsPrimer(UINT32 num);
};


#include "MoonMath.h"
#include <cmath>
MoonMath::MoonMath(void)
{
}
MoonMath::~MoonMath(void)
{
}
template <typename T>
INT32 MoonMath::Sign(T value)
{
if (value > 0)
{
return 1;
}
else if (value == 0)
{
return 0;
}
else
{
return -1;
}
}
bool MoonMath::IsInt(double doubleValue, double epsilon, INT32 &intValue)
{
if (epsilon > 0.5 || epsilon < 0)
{
return false;
}
if(INT32(doubleValue + epsilon) == INT32(doubleValue - epsilon))
{
return false;
}
INT32 value = INT32(doubleValue);
intValue = (fabs(doubleValue - value) > 0.5) ? (value + MoonMath::Sign(doubleValue)) : (value) ;
return true;
}
bool MoonMath::IsPrimer(UINT32 num)
{
// 0和1不是素数
if(num <= 1)
{
return false;
}
UINT32 sqrtOfNum = sqrt((double)num); // num的2次方
// 从2到sqrt(num),如果任何数都不能被num整除,num是素数,否则不是
for(UINT32 i = 2; i <= sqrtOfNum; ++i)
{
if(num % i == 0)
{
return false;
}
}
return true;
}


/*
The Cue Programming Challenge
Level 2
----------------------------------------
Congratulations.  You have reached level 2.
To get the password for level 3, write code to find the first prime
fibonacci number larger than a given minimum.  For example, the first
prime fibonacci number larger than 10 is 13.
When you are ready, go here or call this automated
number (415) 799-9454.
You will receive additional instructions at that time.  For the second portion
of this task, note that for the number 12 we consider the sum of the prime divisors
to be 2 + 3 = 5.  We do not include 2 twice even though it divides 12 twice.
Enter the password to access level 3:
*/
/*
上面的go here:
Step 1. Use your code to compute the smallest prime fibonacci number
greater than 227,000.  Call this number X.
Step 2. The password for level 3 is the sum of prime divisors of X + 1.
Note: If you call the number instead, it will check your answer for step 1.
*/
#include <iostream>
#include <windows.h>
#include <ctime>
#include <set>
#include "MoonMath.h"
using namespace std;
//************************************
// Method:    GetNextFibonacciNum
// Access:    public
// Describe:  获取Fibonacci数列,每次获取都是获取到下一个,因此是一次性函数
// Returns:   UINT32
//************************************
UINT32 GetNextFibonacciNum()
{
static UINT32 lastNum = 0;
static UINT32 currNum = 1;
currNum += lastNum;
lastNum = currNum - lastNum;
return lastNum;
}
UINT32 GetX(UINT32 startNum)
{
UINT32 X;
while((X = GetNextFibonacciNum()) <= startNum);
while(!MoonMath::IsPrimer(X))
{
X = GetNextFibonacciNum();
}
return X;
}
void GetPrimerDivisors(UINT32 num, set<UINT32> &divisors)
{
UINT32 sqrtOfY = sqrt((double)num);
for(UINT32 i = 2; i <= sqrtOfY; ++i)
{
if(!MoonMath::IsPrimer(i))
{
continue;
}
if(num % i == 0)
{
divisors.insert(i);
num /= i;
sqrtOfY = sqrt((double)num);
}
}
if(MoonMath::IsPrimer(num))
{
divisors.insert(num);
}
}
//************************************
// Method:    CalcSum
// Access:    public
// Describe:  计算set容器内数据的和
// Parameter: set<T> contain
// Returns:   T
//************************************
template <typename T>
T CalcSum(set<T> contain)
{
T sum = 0;
for(set<T>::iterator it = contain.begin(); it != contain.end(); ++it)
{
sum += *it;
}
return sum;
}
void F1()
{
cout << "void F1()" << endl;
LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);
/*********************************算法开始*******************************/
UINT32 START_NUM = 227000;
UINT32 X = GetX(START_NUM);
cout << "大于" << START_NUM << "的最小的Fibonacci质数X是:" << X << endl;
UINT32 Y = X + 1;
cout << "Y = X + 1 = " << Y << endl;
// 获得Y的所有不同的质因数
set<UINT32> divisors;
GetPrimerDivisors(Y, divisors);
// 求和
UINT32 sum = CalcSum(divisors);
// 输出
cout << "Y的所有质因数之和为:";
for(set<UINT32>::iterator it = divisors.begin(); it != divisors.end(); it++)
{
cout << *it << " + ";
}
cout << "\b\b= " << sum << endl;
/*********************************算法结束*******************************/
QueryPerformanceCounter(&timeEnd);
cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;
time_t currentTime = time(NULL);
const char BEEP_CHAR = '\007';
const int MAX_TIME_CHAR = 30;
char timeStr[MAX_TIME_CHAR];
ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime);
cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR;
system("pause");
}
//主函数
int main()
{
F1();
return 0;
}
/*
void F1()
大于227000的最小的Fibonacci质数X是:514229
Y = X + 1 = 514230
Y的所有质因数之和为:2 + 3 + 5 + 61 + 281 = 352
Total Milliseconds is 10.7617
By GodMoon
Fri Mar 08 23:23:02 2013
*/


第三题,给一串数字,从中找出一组字串,字串中最大的数字是其他数字之和,求字串的数目。

/*
The Cue Programming Challenge
Level 3
----------------------------------------
Congratulations.  You have reached the final level.
For the final task, you must find all subsets of an array
where the largest number is the sum of the remaining numbers.
For example, for an input of:
(1, 2, 3, 4, 6)
the subsets would be
1 + 2 = 3
1 + 3 = 4
2 + 4 = 6
1 + 2 + 3 = 6
Here is the list of numbers you should run your code on: 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99
The password is the number of subsets.  In the above case the
answer would be 4.
Enter the password to complete the challenge:
给一串数字,从中找出一组字串,字串中最大的数字是其他数字之和,求字串的数目。
英文真啰嗦。
*/
#include <iostream>
#include <windows.h>
#include <ctime>
#include <algorithm>
#include <functional>
using namespace std;
template <typename T>
bool CheckSubset(UINT32 bitMap, const T array[], UINT32 arraySize)
{
if(bitMap == 0)
{
return false;
}
UINT32 index = arraySize - 1;
T maxNum = 0;
T currSum = 0;
// 寻找最大数
while((bitMap & 1) != 1)
{
--index;
bitMap >>= 1;
}
maxNum = array[index];
do
{
bitMap >>= 1;
--index;
if((bitMap & 1) == 1)
{
currSum += array[index];
}
}
while(bitMap != 0);
return currSum == maxNum;
}
// 求数组长度
#define SizeofArray(array) ((sizeof(array))/(sizeof(array[0])))
void F1()
{
cout << "void F1()" << endl;
LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);
/*********************************算法开始*******************************/
const UINT32 BITS_PER_BYTE = 8;
const UINT32 MAX_SUPPORT_LENGTH = sizeof(UINT32) * BITS_PER_BYTE;
const INT32 NUM_ARRAY[] = {3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59,
61, 70, 73, 78, 81, 92, 95, 97, 99};
UINT32 num = SizeofArray(NUM_ARRAY);
if(num > MAX_SUPPORT_LENGTH)
{
cout << "Error: 数组元素太多,无法计算!仅支持最多" << MAX_SUPPORT_LENGTH << "个元素" << endl;
return;
}
// 排序
INT32 *pNumArraySorted = new INT32[num];
(void)memcpy(pNumArraySorted, NUM_ARRAY, num * sizeof(*pNumArraySorted));
sort(pNumArraySorted, pNumArraySorted + num - 1, less<INT32>());
// 遍历所有子串
UINT32 bitMap;
UINT32 maxBitMap = 1 << (num + 1);
UINT32 subsetsNum = 0;
for(bitMap = 1; bitMap < maxBitMap; ++bitMap)
{
if(CheckSubset(bitMap, pNumArraySorted, num))
{
++subsetsNum;
}
}
delete[] pNumArraySorted;
cout << "共有子串" << subsetsNum << "个" << endl;
/*********************************算法结束*******************************/
QueryPerformanceCounter(&timeEnd);
cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;
time_t currentTime = time(NULL);
const char BEEP_CHAR = '\007';  // 计算完毕叫一声
const int MAX_TIME_CHAR = 30;
char timeStr[MAX_TIME_CHAR];
ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime);
cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR;
system("pause");
}
//主函数
int main()
{
F1();
return 0;
}
/*
void F1()
共有子串179个
Total Milliseconds is 1249.17
By GodMoon
Sat Mar 09 08:45:07 2013
*/
/*
The Cue Programming Challenge
The End
----------------------------------------
Congratulations.  You completed the challenge.  Your completion token is *******-***-***-***.
We'd love to talk to you - send your completion token, the code you wrote
during the challenge, and your resume to
jobs+i+solved+the+challenge@cueup.com
Even if you're not looking for a job, we'd love to hear what you thought
about the challenge.
For a new challenge, see if you can complete The Colossal Cue Adventure.
*/



这篇关于http://challenge.greplin.com/上的3道题。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/780128

相关文章

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

【Linux】应用层http协议

一、HTTP协议 1.1 简要介绍一下HTTP        我们在网络的应用层中可以自己定义协议,但是,已经有大佬定义了一些现成的,非常好用的应用层协议,供我们直接使用,HTTP(超文本传输协议)就是其中之一。        在互联网世界中,HTTP(超文本传输协议)是一个至关重要的协议,他定义了客户端(如浏览器)与服务器之间如何进行通信,以交换或者传输超文本(比如HTML文档)。

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

Anaconda 中遇到CondaHTTPError: HTTP 404 NOT FOUND for url的问题及解决办法

最近在跑一个开源项目遇到了以下问题,查了很多资料都大(抄)同(来)小(抄)异(去)的,解决不了根本问题,费了很大的劲终于得以解决,记录如下: 1、问题及过程: (myenv) D:\Workspace\python\XXXXX>conda install python=3.6.13 Solving environment: done.....Proceed ([y]/n)? yDownloa

构建高性能WEB之HTTP首部优化

0x00 前言 在讨论浏览器优化之前,首先我们先分析下从客户端发起一个HTTP请求到用户接收到响应之间,都发生了什么?知己知彼,才能百战不殆。这也是作为一个WEB开发者,为什么一定要深入学习TCP/IP等网络知识。 0x01 到底发生什么了? 当用户发起一个HTTP请求时,首先客户端将与服务端之间建立TCP连接,成功建立连接后,服务端将对请求进行处理,并对客户端做出响应,响应内容一般包括响应

Golang支持平滑升级的HTTP服务

前段时间用Golang在做一个HTTP的接口,因编译型语言的特性,修改了代码需要重新编译可执行文件,关闭正在运行的老程序,并启动新程序。对于访问量较大的面向用户的产品,关闭、重启的过程中势必会出现无法访问的情况,从而影响用户体验。 使用Golang的系统包开发HTTP服务,是无法支持平滑升级(优雅重启)的,本文将探讨如何解决该问题。 一、平滑升级(优雅重启)的一般思路 一般情况下,要实现平滑

Java http请求示例

使用HttpURLConnection public static String httpGet(String host) {HttpURLConnection connection = null;try {URL url = new URL(host);connection = (HttpURLConnection) url.openConnection();connection.setReq

3.比 HTTP 更安全的 HTTPS(工作原理理解、非对称加密理解、证书理解)

所谓的协议 协议只是一种规则,你不按规则来就无法和目标方进行你的工作 协议说白了只是人定的规则,任何人都可以定协议 我们不需要太了解细节,这些制定和完善协议的人去做的,我们只需要知道协议的一个大概 HTTPS 协议 1、概述 HTTPS(Hypertext Transfer Protocol Secure)是一种安全的超文本传输协议,主要用于在客户端和服务器之间安全地传输数据

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException异常

用Gson解析json数据的时候,遇到一个异常,如下图: 这个异常很简单,就是你的封装json数据的javabean没有写对,你仔细查看一下javabean就可以了 比如:我的解析的代码是             Gson gson = new Gson();             ForgetJson rb = gson.fromJson(agResult.mstrJson, For