- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
编辑:我做了主要更改,使用迭代器跟踪位和字符串中的连续位置,并通过 const ref 传递后者。现在,当我多次将示例输入复制到自身上以测试时钟时,对于非常长的位和字符串甚至多达 50 行的示例输入,一切都在 10 秒内完成。但是,当我提交时,CodeEval 说该过程在 10 秒后中止。正如我所提到的,他们不共享他们的输入,所以现在样本输入的“扩展”起作用了,我不确定如何进行。非常感谢任何关于额外改进以提高我的递归性能的想法。
注意:记忆化是一个很好的建议,但我不知道在这种情况下如何实现它,因为我不确定如何在静态查找表中存储位到字符的相关性。我唯一想到的是将位值转换为相应的整数,但是对于长位串来说,这有整数溢出的风险,而且计算起来似乎需要很长时间。对于此处的记忆化的进一步建议也将不胜感激。
这实际上是适度的 CodeEval 挑战之一。他们不共享中等挑战的样本输入或输出,但输出“失败错误”只是说“10 秒后中止”,所以我的代码在某处挂起。
任务很简单。您将文件路径作为单个命令行参数。文件的每一行将包含一系列 0 和 1 以及一系列 A 和 B,以空格分隔。你要根据以下两条规则判断二进制序列是否可以转化为字母序列:
1) 每个0都可以转换为As的任意非空序列(如'A'、'AA'、'AAA'等)
2) 每个 1 都可以转换为 As 或 B 的任何非空序列(例如,'A'、'AA' 等,或 'B'、'BB' 等)(但不是混合的字母)
约束条件是最多处理文件中的 50 行,二进制序列的长度为 [1,150],字母序列的长度为 [1,1000]。
最明显的起始算法是递归执行此操作。我想到的是对于每一位,首先折叠整个下一个允许的字符组,测试缩短的位和字符串。如果失败,一次从被杀死的角色组中添加一个角色,然后再次调用。
这是我的完整代码。为简洁起见,我删除了 cmd 行参数错误检查。
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
using namespace std;
//typedefs
typedef string::const_iterator str_it;
//declarations
//use const ref and iterators to save time on copying and erasing
bool TransformLine(const string & bits, str_it bits_front, const string & chars, str_it chars_front);
int main(int argc, char* argv[])
{
//check there are at least two command line arguments: binary executable and file name
//ignore additional arguments
if(argc < 2)
{
cout << "Invalid command line argument. No input file name provided." << "\n"
<< "Goodybe...";
return -1;
}
//create input stream and open file
ifstream in;
in.open(argv[1], ios::in);
while(!in.is_open())
{
char* name;
cout << "Invalid file name. Please enter file name: ";
cin >> name;
in.open(name, ios::in);
}
//variables
string line_bits, line_chars;
//reserve space up to constraints to reduce resizing time later
line_bits.reserve(150);
line_chars.reserve(1000);
int line = 0;
//loop over lines (<=50 by constraint, ignore the rest)
while((in >> line_bits >> line_chars) && (line < 50))
{
line++;
//impose bit and char constraints
if(line_bits.length() > 150 ||
line_chars.length() > 1000)
continue; //skip this line
(TransformLine(line_bits, line_bits.begin(), line_chars, line_chars.begin()) == true) ? (cout << "Yes\n") : (cout << "No\n");
}
//close file
in.close();
return 0;
}
bool TransformLine(const string & bits, str_it bits_front, const string & chars, str_it chars_front)
{
//using iterators so store current length as local const
//can make these const because they're not altered here
int bits_length = distance(bits_front, bits.end());
int chars_length = distance(chars_front, chars.end());
//check success rule
if(bits_length == 0 && chars_length == 0)
return true;
//Check fail rules:
//1. next bit is 0 but next char is B
//2. bits length is zero (but char is not, by previous if)
//3. char length is zero (but bits length is not, by previous if)
if((*bits_front == '0' && *chars_front == 'B') ||
bits_length == 0 ||
chars_length == 0)
return false;
//we now know that chars_length != 0 => chars_front != chars.end()
//kill a bit and then call recursively with each possible reduction of front char group
bits_length = distance(++bits_front, bits.end());
//current char group tracker
const char curr_char_type = *chars_front; //use const so compiler can optimize
int curr_pos = distance(chars.begin(), chars_front); //position of current front in char string
//since chars are 0-indexed, the following is also length of current char group
//start searching from curr_pos and length is relative to curr_pos so subtract it!!!
int curr_group_length = chars.find_first_not_of(curr_char_type, curr_pos)-curr_pos;
//make sure this isn't the last group!
if(curr_group_length < 0 || curr_group_length > chars_length)
curr_group_length = chars_length; //distance to end is precisely distance(chars_front, chars.end()) = chars_length
//kill the curr_char_group
//if curr_group_length = char_length then this will make chars_front = chars.end()
//and this will mean that chars_length will be 0 on next recurssive call.
chars_front += curr_group_length;
curr_pos = distance(chars.begin(), chars_front);
//call recursively, adding back a char from the current group until 1 less than starting point
int added_back = 0;
while(added_back < curr_group_length)
{
if(TransformLine(bits, bits_front, chars, chars_front))
return true;
//insert back one char from the current group
else
{
added_back++;
chars_front--; //represents adding back one character from the group
}
}
//if here then all recursive checks failed so initial must fail
return false;
}
他们给出了以下测试用例,我的代码正确解决了这些用例:
示例输入:
1| 1010 AAAABBBBAAAA
2| 00 AAAAAA
3| 01001110 AAAABAAABBBBBBAAAAAAA
4| 1100110 BBAABABBA
正确的输出:
1|是的
2|是的
3|是的
4|否
由于转换是可能的,当且仅当它的拷贝存在时,我尝试将每个二进制和字母序列多次复制到自身上,并查看时钟如何运行。即使对于非常长的位和字符串以及许多行,它也能在 10 秒内完成。
我的问题是:由于 CodeEval 仍然说它的运行时间超过 10 秒,但他们不分享他们的输入,是否有人有任何进一步的建议来提高此递归的性能?或者可能是一种完全不同的方法?
预先感谢您的帮助!
最佳答案
这是我发现的:
通过常量引用传递
字符串和其他大型数据结构应通过常量引用传递。
这允许编译器将指针传递给原始对象,而不是复制数据结构。
调用函数一次,保存结果
您正在调用 bits.length()
两次。您应该调用一次并将结果保存在常量变量中。这允许您在不调用函数的情况下再次检查状态。
对于时间紧迫的程序来说,函数调用是昂贵的。
使用常量变量
如果您不打算在赋值后修改变量,请使用 const
在声明中:
const char curr_char_type = chars[0];
const
允许编译器执行更高阶的优化并提供安全检查。
改变数据结构
由于您可能在字符串中间执行插入,因此您应该为字符使用不同的数据结构。 std::string
数据类型可能需要在插入后重新分配并将字母进一步向下移动。使用 std::list<char>
插入速度更快因为链表只交换指针。可能会有折衷,因为链表需要为每个字符动态分配内存。
在字符串中保留空间
当您创建目标字符串时,您应该使用一个构造函数来为最大大小的字符串预分配或保留空间。这将防止 std::string
从重新分配。重新分配的成本很高。
不要删除
你真的需要删除字符串中的字符吗?通过使用开始和结束索引,您可以覆盖现有字母而不必删除整个字符串。部分删除是昂贵的。完全删除不是。
如需更多帮助,请在 StackExchange 上发布代码审查。
关于c++ - 递归字符串转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313989/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!