320. Generalized Abbreviation

Problem

Write a function to generate the generalized abbreviations of a word.

Example:

Given word = "word", return the following list (order does not matter):

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]


Solution 

典型的 backtracking, 唯一要注意的是例子给的单词长度是各位数,要考虑单词很长的时候可以是两位数,这样就不能用  num + '0' 来表示当前的数字字符了。

class Solution {
    void helper(int begin, string& oneRst, vector<string>& rst, const string& word ){
        
        if(begin == word.size()) {
            rst.push_back(oneRst);
            return;
        }
        
        oneRst.push_back(word[begin]);
        helper(begin + 1, oneRst, rst, word);
        oneRst.pop_back();
        
        if( oneRst.empty() ||  oneRst.back() < '0' || oneRst.back() > '9' ){
            for( int numLen = 1; numLen <= word.size() - begin; numLen++) {
                string str = to_string(numLen);
                oneRst.append(str);
                helper(begin + numLen, oneRst, rst, word);
                oneRst.erase(oneRst.end() - str.size(), oneRst.end());
            }
        }
    }
    
public:
    vector<string> generateAbbreviations(string word) {
        string oneRst; 
        vector<string> rst;
        helper(0, oneRst, rst, word);
        return rst;
    }
};


Generalized Extreme Value(广义极值分布)是一种连续型概率分布,通常用于描述极端值的分布。它的概率密度函数为:$$f(x)=\frac{1}{\sigma}\Bigg[\frac{x-\mu}{\sigma}\Bigg]^{-1-\xi}e^{-\Big[\frac{x-\mu}{\sigma}\Big]^{-\xi}}$$ 其中,$\mu$是分布的位置参数,$\sigma>0$是分布的尺度参数,$\xi$是分布的形状参数。 下面是一些与GEV分布相关的操作: 1. GEV分布的概率密度函数 ```python import numpy as np import matplotlib.pyplot as plt from scipy.stats import genextreme loc, scale, shape = 0.5, 2, -0.1 # 分布的参数 x = np.linspace(-2, 6, 1000) pdf = genextreme.pdf(x, shape, loc, scale) # GEV分布的概率密度函数 plt.plot(x, pdf, label='shape = '+str(shape)) # 绘制概率密度函数曲线 plt.xlabel('x') plt.ylabel('pdf(x)') plt.legend() plt.show() ``` 2. GEV分布的累积分布函数 ```python import numpy as np import matplotlib.pyplot as plt from scipy.stats import genextreme loc, scale, shape = 0.5, 2, -0.1 # 分布的参数 x = np.linspace(-2, 6, 1000) cdf = genextreme.cdf(x, shape, loc, scale) # GEV分布的累积分布函数 plt.plot(x, cdf, label='shape = '+str(shape)) # 绘制累积分布函数曲线 plt.xlabel('x') plt.ylabel('cdf(x)') plt.legend() plt.show() ``` 3. GEV分布的样本随机数生成 ```python import numpy as np from scipy.stats import genextreme loc, scale, shape = 0.5, 2, -0.1 # 分布的参数 rv = genextreme(shape, loc, scale) # GEV分布的随机变量生成器 data = rv.rvs(size=1000) # 生成1000个随机样本 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值