bijection

290. Word Pattern【LeetCode by java】

青春壹個敷衍的年華 提交于 2021-02-06 07:46:43
今天发现LintCode页面刷新不出来了,所以就转战LeetCode。还是像以前一样,做题顺序:难度从低到高,每天至少一题。 Given a pattern and a string str , find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str . Example 1: Input: pattern = "abba" , str = "dog cat cat dog" Output: true Example 2: Input:pattern = "abba" , str = "dog cat cat fish" Output: false Example 3: Input: pattern = "aaaa" , str = "dog cat cat dog" Output: false Example 4: Input: pattern = "abba" , str = "dog dog dog dog" Output: false Notes: You may assume pattern contains only

【读书笔记】排列研究-逆序对

天大地大妈咪最大 提交于 2020-08-17 16:12:11
目录 生成函数 一开始的一些值 递归方程 递归方程-续 由生成函数找$b(n,k)$的显式表达式 Explicit formula 书里所给证明的思路 major index 联系行列式很是典型的那种定义 联系二分图的完美匹配 多重集构成的排列的逆序对 生成函数 举例,比如 \(n=3\) 排列有123,132,213,231,312,321 逆序对数分别是0,1,1,2,2,3 \[x^0+x^1+x^1+x^2+x^2+x^3=(1+x)(1+x+x^2) \] 课本给出的一个简单的证明是使用数学归纳法: 当 \(n=2\) 时,当然 \(1+x\) 如果对 \(n-1\) 成立的话,那么考虑n-1排列的 \(n\) 个位置插入元素 \(n+1\) ,分别会使逆序对数+0,+1,+2,+3,...,+(n-1)。这就解释了GF又乘上 \(1+x+...+x^{n-1}\) 记 \(b(n,k)\) 是 \(I_n(x)\) 的 \(x^k\) 前的系数,也是长度为 \(n\) 的逆序对数为 \(k\) 的排列的个数 一开始的一些值 递归方程 此递归方程的适用条件是 \(n\geq k\) 证明是说考察n+1-排列的最后一个元素: 如果是 \(n+1\) ,那么 \(n+1\) 在的数对不贡献, 所以【n+1-排列且k个逆序对,with最后一个元素是 \(n+1\) 】的数目和

Is there “good” PRNG generating values without hidden state?

独自空忆成欢 提交于 2020-01-01 09:25:17
问题 I need some good pseudo random number generator that can be computed like a pure function from its previous output without any state hiding. Under "good" I mean: I must be able to parametrize generator in such way that running it for 2^n iterations with any parameters (or with some large subset of them) should cover all or almost all values between 0 and 2^n - 1 , where n is the number of bits in output value. Combined generator output of n + p bits must cover all or almost all values between

Create a random bijective function which has same domain and range

狂风中的少年 提交于 2019-12-11 08:03:49
问题 Create a random bijective function which has same domain and range. By random bijective function I mean a function which maps the elements from domain to range using a random algorithm (or at least a pseudo-random algo), and not something like x=y. The domain and range can be a very small set sometimes like {1,2,3,4,5}, so a pairing function won't work. Also, computational resources are limited, so it should be a computationaly feasible function. Edit: What I meant in the question was, say

Find possible bijection between characters and digits

瘦欲@ 提交于 2019-12-08 19:34:13
问题 Let's say you have a string S and a sequence of digits in a list L such that len(S) = len(L). What would be the cleanest way of checking if you can find a bijection between the characters of the string to the digits in the sequence such that each character matches to one and only one digit. For example, "aabbcc" should match with 115522 but not 123456 or 111111. I have a complex setup with two dicts and loop, but I'm wondering if there's a clean way of doing this, maybe by using some function

Bijective “Integer <-> String” function

心已入冬 提交于 2019-12-07 06:15:54
问题 Here's a problem I'm trying to create the best solution for. I have a finite set of non-negative integers in the range of [0...N] . I need to be able to represent each number in this set as a string and be able to convert such string backwards to original number. So this should be a bijective function. Additional requirements are: String representation of a number should obfuscate original number at least to some degree. So primitive solution like f(x) = x.toString() will not work. String

Is there an established way to write parsers that can reconstruct their exact input?

此生再无相见时 提交于 2019-12-06 19:29:58
问题 Say I want to parse a file in language X . Really, I'm only interested in a small part of the information within. It's easy enough to write a parser in one of Haskell's many eDSLs for that purpose (e.g. Megaparsec). data Foo = Foo Int -- the information I'm after. parseFoo :: Parsec Text Foo parseFoo = ... That readily gives rise to a function getFoo :: Text -> Maybe Foo . But now I would also like to modify the source of the Foo information, i.e. basically I want to implement changeFoo ::

Bijective “Integer <-> String” function

天大地大妈咪最大 提交于 2019-12-05 10:34:40
Here's a problem I'm trying to create the best solution for. I have a finite set of non-negative integers in the range of [0...N] . I need to be able to represent each number in this set as a string and be able to convert such string backwards to original number. So this should be a bijective function. Additional requirements are: String representation of a number should obfuscate original number at least to some degree. So primitive solution like f(x) = x.toString() will not work. String length is important: the less the better. If one knows the string representation of K , I would like it to

Pseudo-random-looking one-to-one int32->int32 function

人走茶凉 提交于 2019-11-30 09:37:16
I am looking for a int32->int32 function that is bijection (one-to-one correspondence) cheap to calculate at least in one direction transforms the increasing sequence 0, 1, 2, 3, ... into a sequence looking like a good pseudo-random sequence (~ half bits flip when argument changes by a small number, no obvious patterns) Multiply by a large odd number and xor with a different one. Bijection: odd numbers have a multiplicative inverse modulo powers of two, so the multiplication is undone by a multiplication by the inverse. And xor is, of course, undone by another xor. This is basically how the

Pseudo-random-looking one-to-one int32->int32 function

别说谁变了你拦得住时间么 提交于 2019-11-29 14:22:05
问题 I am looking for a int32->int32 function that is bijection (one-to-one correspondence) cheap to calculate at least in one direction transforms the increasing sequence 0, 1, 2, 3, ... into a sequence looking like a good pseudo-random sequence (~ half bits flip when argument changes by a small number, no obvious patterns) 回答1: Multiply by a large odd number and xor with a different one. Bijection: odd numbers have a multiplicative inverse modulo powers of two, so the multiplication is undone by