- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在寻找一些关于如何找到其字符可能有多个变体的字符串的所有可能版本的提示。
一个简单的例子:“澳门”是起始字符串。字符“a”有变体“ä”,字符“o”有变体“ö”。
目标是从上面的信息中得到如下列表:
Macao
Mäcao
Macäo
Mäcäo
Macaö
Mäcaö
Macäö
Mäcäö
到目前为止,我的方法是识别和提取具有变体的字符以简化操作。这个想法是处理各个字符而不是整个单词。
aao
äao
aäo
ääo
aaö
äaö
aäö
ääö
以下代码查找我们正在使用的变体。
std::vector<std::string> variants;
variants.push_back("aä");
variants.push_back("oö");
std::string word = "Macao";
std::vector<std::string> results;
for (auto &variant : variants) {
for (auto &character : word) {
if (variant.front() == character) {
results.push_back(variant);
}
}
}
std::cout << "The following characters have variants: ";
for (auto &i : results) {
std::cout << i.front();
}
std::cout << std::endl;
下一步是找到各个字符的所有可能组合。为此,我写了下面的函数。它根据 results
中每个字符串的第一个字符创建一个新字符串。
std::string read_results(std::vector<std::string> &results)
{
std::string s;
for (auto &c : results) {
s.push_back(c.front());
}
return s;
}
我的想法是,然后更改存储在 results
中的字符串,以获得所有可能的组合,这就是我遇到的问题。我注意到 std::rotate
似乎会有帮助。
最佳答案
倒排索引可能会有用。
您可以将具有多个变体的所有字母按顺序存储在一个 vector 中,并为每个字母创建一个具有分组索引的 vector ,以便第 i 个字母属于组 I[i]
,所有索引与 I[i]
相同的字母都是同一字母的变体:
string L = "aoäöâô"; // disclaimer: i don't know if this is really in order
unsigned int I[] = {0,1,0,1,0,1};
// this means that "aäâ" belong to group 0, and "oöô" belong to group 1
你可以为前面的 L
和 I
建立倒排索引,像这样:
vector<vector<unsigned int> > groups;
// groups[k] stores the indices in L of the letters that belongs to the k-th group.
// do groups.reserve to make this operation more efficient
for(size_t i = 0; i < L.size(); ++i)
{
unsigned int idx = I[i];
if(idx <= groups.size()) groups.resize(idx+1);
groups[idx].push_back(i);
}
L
中的字母按顺序排列很重要,因此您稍后可以对其进行二进制搜索,这需要 O(logn)
而不是 通常循环的 O(n)
。然后,一旦你有了你的字母组,你就可以用倒排索引找到它的变体:
char letter = 'a';
string::iterator it = std::lower_bound(L.begin(), L.end(), letter);
if(it != L.end() && *it == letter)
{
unsigned int idx = I[ it - L.begin() ];
// the letter has variants because it belongs to group idx
const vector<unsigned int>& group = groups[idx];
for(vector<unsigned int>::const_iterator git = group.begin();
git != group.end(); ++git)
{
// now, L[*git] is one of the variants of letter
...
}
}
关于c++ - 查找其字符可能有多个变体的字符串的所有可能版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19156847/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!