string matching(HDU-6629)

Problem Description

String matching is a common type of problem in computer science. One string matching problem is as following:

Given a string s[0…len−1], please calculate the length of the longest common prefix of s[i…len−1] and s[0…len−1] for each i>0.

I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:

We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.

Input

The first line contains an integer T, denoting the number of test cases.
Each test case contains one string in a line consisting of printable ASCII characters except space.

* 1≤T≤30

* string length ≤106 for every string

Output

For each test, print an integer in one line indicating the number of compare operations invoked if we run the algorithm in the statement against the input string.

Sample Input

3
_Happy_New_Year_
ywwyww
zjczzzjczjczzzjc

Sample Output

17
7
32

题意:t 组数据,每组给出一个字符串 S,现在有一个函数,每次会暴力的跑 S 的所有后缀与字符串 S 的最长公共前缀,问这个函数会运行多少次

思路:

实质是求 S 的每个后缀与 S 的最长公共前缀的比较次数之和

扩展 KMP 模版题,求出 extend 数组后比较累加即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int Next[N];
int extend[N];
void getNext(char *str) {
    int len = strlen(str);
    Next[0] = len;
    int i = 0;
    while (str[i] == str[i + 1] && i + 1 < len)
        i++;
    Next[1] = i;

    int pos = 1;
    for (i = 2; i < len; i++) {
        if (Next[i - pos] + i < Next[pos] + pos)
            Next[i] = Next[i - pos];
        else {
            int j = Next[pos] + pos - i;
            if (j < 0)
                j = 0;
            while (i + j < len && str[j] == str[j + i])
                j++;
            Next[i] = j;
            pos = i;
        }
    }
}
void exKMP(char *s1, char *s2) {
    getNext(s2);
    int len1 = strlen(s1);
    int len2 = strlen(s2);

    int i = 0;
    while (s1[i] == s2[i] && i < len2 && i < len1)
        i++;
    extend[0] = i;

    int pos = 0;
    for (i = 1; i < len1; i++) {
        if (Next[i - pos] + i < extend[pos] + pos)
            extend[i] = Next[i - pos];
        else {
            int j = extend[pos] + pos - i;
            if (j < 0)
                j = 0;
            while (i + j < len1 && j < len2 && s1[j + i] == s2[j])
                j++;
            extend[i] = j;
            pos = i;
        }
    }
}

char s[N];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(Next,0,sizeof(Next));
        memset(extend,0,sizeof(extend));

        scanf("%s",s);
        exKMP(s,s);
        
        LL res=0;
        int len=strlen(s);
        for(int i=1;i<len;i++){
            res+=extend[i]+1;
            if(extend[i]+i>=len)
                res--;
        }
        printf("%lld\n",res);
    }
    return 0;
}

 

<think>好的,我现在需要帮助用户解决npm安装时出现的“notarget No matching version found for string-width-cjs@^4.2.3”错误。首先,我要理解这个错误的原因。根据引用内容,这个错误通常是因为请求的包版本不存在,或者镜像源的问题。 首先,用户提到的包是string-width-cjs@^4.2.3。我需要检查这个包是否存在,以及是否有这个版本。根据引用[5]的建议,应该先去npm官网搜索这个包。现在,我需要假设搜索的结果。例如,可能用户拼错了包名,或者版本号不正确。例如,string-width-cjs可能是一个分支或特定构建的包,而正确的包名可能是string-width,或者是否有4.2.3这个版本。 另外,根据引用[4]的例子,用户可能使用的包版本在镜像源中不存在,需要更换版本。例如,xlsx@0.8.1不存在,但其他版本存在,所以需要修改package.json中的版本号。同样,这里可能需要用户检查string-width-cjs的可用版本,是否存在4.2.3,或者是否有拼写错误。 另外,引用[5]提到镜像源的问题。如果用户使用了非官方镜像,可能某些包没有同步,导致找不到版本。这时候需要切换回npm官方源或者检查镜像是否包含所需的包。 此外,引用[1]、[2]、[3]中的情况,可能问题出在依赖链中,某个依赖项请求了一个不存在的版本。例如,项目中的一个依赖可能要求string-width-cjs@^4.2.3,但实际上这个版本不存在,或者包名错误。这时候需要确认是否是直接依赖还是间接依赖,并更新相应的包版本。 可能的解决步骤包括: 1. 确认包名是否正确,是否存在拼写错误。比如,是否应该是string-width而不是string-width-cjs? 2. 检查该包的可用版本,使用npm view string-width-cjs versions查看是否存在4.2.3。 3. 如果版本不存在,修改package.json中的版本号为一个存在的版本。 4. 检查是否是镜像源问题,更换为官方源或其他镜像。 5. 清除npm缓存后重新安装。 6. 如果问题出在子依赖,可能需要更新父依赖或联系维护者。 现在,我需要将这些步骤整理成用户友好的解答,并引用相关的引用内容来支持建议。例如,当建议检查版本是否存在时,可以引用引用[4]中的例子,使用npm view命令。当提到镜像源问题时,引用引用[5]的建议。同时,需要指导用户如何执行这些步骤,比如如何更换镜像源,如何修改package.json等。 可能还需要注意,string-width-cjs可能是一个CommonJS版本的包,而原包string-width可能有不同的版本结构。或者是否存在改名的情况。比如,是否该包已经迁移或更名,导致无法找到。这时候用户需要查找正确的包名和版本。 总结来说,解决步骤应该是逐步排查包名、版本、镜像源、缓存等问题,然后根据具体情况调整依赖版本或配置。</think>### 解决 `npm ERR! notarget No matching version found for string-width-cjs@^4.2.3` 的方法 #### 1. **确认包名和版本是否存在** - **检查包名拼写**:首先确认包名 `string-width-cjs` 是否正确。可能存在拼写错误,例如原包名可能是 `string-width`(无 `-cjs` 后缀)[^5]。 - **查看可用版本**:运行以下命令检查该包的已发布版本: ```bash npm view string-width-cjs versions ``` 如果返回空或未包含 `4.2.3`,则说明版本不存在(参考引用[4]的案例)。 #### 2. **修改依赖版本** - 如果版本不存在,需修改 `package.json` 中的依赖版本。例如: ```json "dependencies": { "string-width-cjs": "^4.2.4" // 替换为实际存在的版本 } ``` - 若包名错误(如应为 `string-width`),需更正包名并选择其有效版本。 #### 3. **检查镜像源问题** - 若包存在但安装失败,可能是镜像源未同步。切换为 npm 官方源: ```bash npm config set registry https://registry.npmjs.org ``` 之后重新运行 `npm install`(参考引用[5])。 #### 4. **清理缓存并重试** - 清除 npm 缓存后重新安装: ```bash npm cache clean --force rm -rf node_modules package-lock.json npm install ``` #### 5. **检查子依赖问题** - 如果该包是其他依赖的子依赖(非直接依赖),尝试更新父级包版本: ```bash npm update <parent-package> ``` 或联系父级包的维护者反馈版本问题(参考引用[1][2][3])。 #### 6. **验证包的真实性** - 访问 [npm 官网](https://www.npmjs.com/) 搜索 `string-width-cjs`,确认包是否被删除或更名。若已废弃,需寻找替代方案。 --- ### 相关问题 1. **如何避免 npm 安装时出现版本不匹配问题?** - 建议使用 `npm install <package>@latest` 安装最新稳定版本,或在 `package.json` 中指定明确的版本号(如 `1.2.3` 而非 `^1.2.3`)。 2. **如何快速检查 npm 包的可用版本?** - 使用 `npm view <package> versions` 命令,或通过 npm 官网的版本历史页面查询[^4][^5]。 3. **镜像源导致依赖安装失败时,如何恢复默认配置?** - 执行 `npm config set registry https://registry.npmjs.org` 切换回官方源,或使用工具如 `nrm` 管理镜像源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值