我正在制作一个需要数千个快速字符串查找和前缀检查的移动应用程序.为了加快速度,我从单词列表中创建了一个Trie,它有大约180,000个单词.
一切都很棒,但唯一的问题是,构建这个巨大的trie(它有大约400,000个节点)目前在我的手机上大约需要10秒,这真的很慢.
这是构建trie的代码.
public SimpleTrie makeTrie(String file) throws Exception {
String line;
SimpleTrie trie = new SimpleTrie();
BufferedReader br = new BufferedReader(new FileReader(file));
while( (line = br.readLine()) != null) {
trie.insert(line);
}
br.close();
return trie;
}
Run Code Online (Sandbox Code Playgroud)
insert运行的方法O(length of key)
public void insert(String key) {
TrieNode crawler = root;
for(int level=0 ; level < key.length() ; level++) {
int index = key.charAt(level) - 'A';
if(crawler.children[index] == null) {
crawler.children[index] = getNode();
}
crawler = crawler.children[index];
}
crawler.valid …Run Code Online (Sandbox Code Playgroud) 我有一个Python脚本只有这两行:
import requests
print len(dir(requests))
Run Code Online (Sandbox Code Playgroud)
它打印:
12
48
Run Code Online (Sandbox Code Playgroud)
当我打印实际列表时dir(requests),我得到了这个:
['__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__']
['ConnectionError', 'HTTPError', 'NullHandler', 'PreparedRequest', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'adapters', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'utils']
Run Code Online (Sandbox Code Playgroud)
我猜有多个requests模块或类似的东西.请帮忙.
我需要找到给定列表中三角形内的点数.这里的问题是,可能有多达一百万点.
我尝试了一种简单的方法:如果三角形的面积等于通过一次取两个三角形点形成的3个三角形的面积之和和要检查的点,则在其内部.这没有任何精度错误,因为我没有除以2来找到该区域.
但我需要更快的东西.目标是速度.是否可以通过某种预处理使其更快,忽略某些基于某些标准或类似的点?
编辑:忘了添加一个关键细节.一旦给出的点数是固定的.然后这些点是静态的,需要检查多达一百万个三角形......
编辑2:证明一个好的(也许是最佳的)方法是使用线扫描.不过,谢谢你的回答!
我有这个C结构:(代表一个IP数据报)
struct ip_dgram
{
unsigned int ver : 4;
unsigned int hlen : 4;
unsigned int stype : 8;
unsigned int tlen : 16;
unsigned int fid : 16;
unsigned int flags : 3;
unsigned int foff : 13;
unsigned int ttl : 8;
unsigned int pcol : 8;
unsigned int chksm : 16;
unsigned int src : 32;
unsigned int des : 32;
unsigned char opt[40];
};
Run Code Online (Sandbox Code Playgroud)
我正在为它赋值,然后以16位字打印它的内存布局,如下所示:
//prints 16 bits at a time
void print_dgram(struct ip_dgram dgram)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个生成Windows命令的小Python脚本:
def quoted(s):
return '"' + s + '"'
import os
path = 'C:\\Program Files\\SumatraPDF\\SumatraPDF.exe'
params = ' -page 5 '
arg = 'D:\\Dropbox\\Final Term\\Final Draft.pdf'
cmd = quoted(path) + params + quoted(arg)
print cmd
os.system(cmd)
Run Code Online (Sandbox Code Playgroud)
这不会在Sublime Text 2中运行(按Ctrl+ B):
"C:\Program Files\SumatraPDF\SumatraPDF.exe" -page 5 "D:\Dropbox\Final Term\Final Draft.pdf"
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
[Finished in 0.1s]
Run Code Online (Sandbox Code Playgroud)
但如果我手动复制并粘贴命令(由此脚本输出),则运行cmd.exe.
我如何使其工作?
python windows command-line-interface sublimetext sublimetext2
我正在std::shared_ptr<void>我的应用程序中使用一个智能指针,它可以指向许多不同类型的数据结构,如结构,向量,矩阵......基本上任何东西.我要做的是将一些名称映射到他们的数据结构.
我正在使用哈希表执行映射:
std::unordered_map<std::string, std::shared_ptr<void>>
我可以把std::shared_ptr<void>返回的人投find()回去std::shared_ptr<my_type>吗?如果是这样,怎么样?
更重要的是,这是一种好的做法吗?随着应用程序的扩展,这会增加复杂性吗?或者,还有其他一些完全不同,优雅的方法吗?
编辑
可能不能使用`Boost.Any',因为它使用RTTI.
也不能为所有这些数据结构使用基类,因为它们中的一些是STL容器std::vector.
关于在shared_ptr下面的答案中讨论的删除问题,我读到shared_ptr执行类型擦除并存储类型信息以知道要调用的析构函数.
但我不确定这一点.
我std::vector用来在Image类中存储图像.我理解他们的工作方式有点麻烦.旋转图像的功能:
void Image :: resize (int width, int height)
{
//the image in the object is "image"
std::vector<uint8_t> vec; //new vector to store rotated image
// rotate "image" and store in "vec"
image = vec; // copy "vec" to "image" (right?)
//vec destructs itself on going out of scope
}
Run Code Online (Sandbox Code Playgroud)
有没有办法阻止最后一个副本?就像在Java中一样,只需切换引用?如果防止任何复制将是很好的.
我正在尝试这个简单的代码:
import requests
print requests.__file__
r = requests.get('https://github.com/timeline.json')
Run Code Online (Sandbox Code Playgroud)
当我逐行输入行时,它在命令行上完美地工作,但当我作为脚本或Sublime Text 2执行它时,它不是什么.这是堆栈跟踪:
C:\Python27\lib\site-packages\requests\__init__.pyc
Traceback (most recent call last):
File "C:\Users\Bruce\Desktop\http.py", line 1, in <module>
import requests
File "C:\Python27\lib\site-packages\requests\__init__.py", line 53, in <module>
from requests.packages.urllib3.contrib import pyopenssl
File "C:\Python27\lib\site-packages\requests\packages\__init__.py", line 3, in <module>
from . import urllib3
File "C:\Python27\lib\site-packages\requests\packages\urllib3\__init__.py", line 16, in <module>
from .connectionpool import (
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 15, in <module>
from http.client import HTTPConnection, HTTPException
File "C:\Users\Bruce\Desktop\http.py", line 3, in <module>
r = requests.get('https://github.com/timeline.json')
AttributeError: 'module' object has no …Run Code Online (Sandbox Code Playgroud) python ×3
algorithm ×2
c++ ×2
bit-fields ×1
c ×1
c++11 ×1
geometry ×1
ip ×1
optimization ×1
performance ×1
python-2.7 ×1
shared-ptr ×1
stdvector ×1
stl ×1
struct ×1
sublimetext ×1
sublimetext2 ×1
trie ×1
unix ×1
vector ×1
windows ×1