作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试解决 UVA 417 ,但我无法这样做。我见过的所有解决方案首先生成所有可能的值,将它们存储在映射中,然后搜索以找到所需的字符串。这对我来说似乎很不优雅。没有办法从数学上解决这个问题吗?
考虑输入“abc”。
如果不强加每个后续字符都应该大于当前字符的条件,我们可以通过简单地计算 1*26^2 +2*26^1 + 3*26^0 来解决它。没有办法以类似的方式解决原始问题吗?
我包含了我在网上找到的现有解决方案的代码:
#include <iostream>
#include <string>
#include <map>
#include <queue>
using namespace std;
map<string, int> M;
void generate_positions(){
queue<string> Q;
for(char c='a';c<='z';c++) Q.push(string(1,c));
string s;
int cont=1;
while(!Q.empty()){
s=Q.front();
Q.pop();
M[s]=cont;
cont++;
if(s.size()==5) continue;
for(char c=s[s.size()-1]+1;c<='z';c++) Q.push(s+c);
}
}
int main(){
generate_positions();
string s;
map<string, int> :: iterator it;
while(cin>>s){
it=M.find(s);
if(it==M.end()) cout<<0<<endl;
else cout<<it->second<<endl;
}
return 0;
}
最佳答案
1. TL/DR:
让我们定义一个字母表 A = {a, b, c, ... z} 及其双射字母表 A' = {1, 2, 3, ... 26} . |A| = |A'| = 26 .
然后让 w 表示由字符组成的词 w0, w1, ... w|w|–1 ∈ A' ,按以下顺序:
w = w|w|–1 ... w1w0 ,其中 |w| 表示以字符为单位的字长。
现在,functional P(w): (A')|A'| → ℕ 转换单词 w, |w| ≤ |A'| ,其位置如下:
音译周至 A' 来自 一个 取决于用户。
2. 实现:
#include <stdio.h>
#include <math.h>
long bin26(long n, long k) {
static const char nprm[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
long root = (long)sqrt(n), coef[sizeof(nprm) / sizeof(*nprm)] = {0};
long indx, iter, prod, curr, prev, ncur, kcur;
if ((n <= 0) || ((k = (k > n / 2)? n - k : k) <= 0))
return (n > 0) && (k >= 0);
for (indx = iter = 0; (curr = (long)nprm[iter++]) <= n; )
if (curr > n - k)
coef[indx++] = curr;
else if (curr <= n / 2) {
if (curr > root) {
if ((n % curr) < (k % curr))
coef[indx++] = curr;
continue;
}
for (ncur = n, kcur = k, prod = 1, prev = 0; ncur > 0; ) {
if ((prev = ((ncur % curr) < (kcur % curr + prev))? 1 : 0))
prod *= curr;
ncur /= curr;
kcur /= curr;
}
if (prod > 1)
coef[indx++] = prod;
}
for (iter = 1; indx; iter *= coef[--indx]);
return iter;
}
int main(int argc, char *argv[]) {
const long size = 26;
long retn, lstr, iter;
for (--argc; argc > 0; argc--) {
for (iter = lstr = 0; argv[argc][lstr]; iter = argv[argc][lstr++])
if (iter >= argv[argc][lstr]) {
lstr = 0;
break;
}
for (--lstr, iter = retn = 0; iter <= lstr; iter++)
retn += bin26(size, iter + 1)
- bin26(size - argv[argc][lstr - iter] + 'a' - 1, iter + 1);
printf("P(%s) = %ld\n", argv[argc], retn);
}
return 0;
}
k n [ | ] be → 14 [ | ] acf → 28
[1|1] a → 1 [ | ] bf → 15 [ | ] ade → 29
[ |1] b → 2 [ | 3] cd → 16 [ | ] adf → 30
[ |1] c → 3 [ | ] ce → 17 [ | ] aef → 31
[ |1] d → 4 [ | ] cf → 18 [ |6] bcd → 32
[ |1] e → 5 [ | 2] de → 19 [ | ] bce → 33
[ |1] f → 6 [ | ] df → 20 [ | ] bcf → 34
[2|5] ab → 7 [ | 1] ef → 21 [ | ] bde → 35
[ | ] ac → 8 [3|10] abc → 22 [ | ] bdf → 36
[ | ] ad → 9 [ | ] abd → 23 [ | ] bef → 37
[ | ] ae → 10 [ | ] abe → 24 [ |3] cde → 38
[ | ] af → 11 [ | ] abf → 25 [ | ] cdf → 39
[ |4] bc → 12 [ | ] acd → 26 [ | ] cef → 40
[ | ] bd → 13 [ | ] ace → 27 [ |1] def → 41
...
|w–2|
---------
|w–1|
|w–1|
|w–1|
|w–1|
|w–1|
|w–1|
---------
| w |
| w |<--- P(w)
| w |
| w |
| w |
-----<--- P₁(w)
|w+1|
...
关于c++ - 有没有办法在数学上解决 UVA 417?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54438269/
我是一名优秀的程序员,十分优秀!