鱼C论坛

 找回密码
 立即注册
查看: 929|回复: 2

[已解决]codeforcess上一道题超时了,应该怎么修改

[复制链接]
发表于 2023-11-13 00:56:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <bits/stdc++.h>
using namespace std;
int n;
const int N=101;
string a[N];
int main()
{
    cin>>n;
    string s;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
        int pos=-1;
        int cnt=0,ans=0;
        string c="ab";
        string d="ba";
        while((pos=a[i].find(c,pos+1))!=-1)
        {
            cnt++; //
        }
        while ((pos=a[i].find(d,pos+1))!=-1)
        {
            ans++;
        }
    
        if(a[i].size()==1)cout<<a[i]<<endl;
        else
        {
            if(cnt!=ans)
            {
                a[i][0]='b';
                cout<<a[i]<<endl;
            }
            else cout<<a[i]<<endl;
          
            

        }
        
        
    }
    



}
最佳答案
2023-11-13 00:57:43
这段代码中出现超时的问题主要是在字符串查找部分的逻辑,导致时间复杂度较高。为了提高效率,可以对字符串查找部分进行优化,同时简化冗余的逻辑。

以下是对代码的修改建议:
#include <iostream>#include <string>using namespace std;int main() {    int n;    const int N = 101;    string a[N];    cin >> n;    for (int i = 0; i < n; i++) {        cin >> a[i];    }    for (int i = 0; i < n; i++) {        int cnt_ab = 0, cnt_ba = 0;        for (int j = 0; j < a[i].size() - 1; j++) {            if (a[i][j] == 'a' && a[i][j + 1] == 'b') {                cnt_ab++;            }            if (a[i][j] == 'b' && a[i][j + 1] == 'a') {                cnt_ba++;            }        }        if (a[i].size() == 1) {            cout << a[i] << endl;        } else {            if (cnt_ab != cnt_ba) {                a[i][0] = 'b';            }            cout << a[i] << endl;        }    }    return 0;}
这样修改后的代码应该能够避免超时问题,并且更加高效地统计子串出现的次数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-13 00:56:55 | 显示全部楼层
这段代码的问题在于使用了两个嵌套的while循环来遍历字符串中的子串,这种方法在字符串较长时会导致超时。为了解决这个问题,可以使用一个更高效的算法来统计字符串中特定子串出现的次数。

一个更高效的方法是使用KMP算法来在字符串中搜索子串的出现次数。KMP算法可以在O(n+m)的时间复杂度内完成搜索,其中n是主串的长度,m是模式串(子串)的长度。

下面是修改后的代码:
#include <bits/stdc++.h>
using namespace std;

int n;
const int N=101;
string a[N];

// KMP算法中的next数组计算函数
vector<int> getNext(string pattern) {
    int m = pattern.size();
    vector<int> next(m, 0);
    for (int i = 1, j = 0; i < m; i++) {
        while (j > 0 && pattern[ i] != pattern[j]) {
            j = next[j - 1];
        }
        if (pattern[ i] == pattern[j]) {
            j++;
        }
        next[ i] = j;
    }
    return next;
}

// KMP算法
int kmp(string text, string pattern) {
    int n = text.size(), m = pattern.size();
    vector<int> next = getNext(pattern);
    int cnt = 0;
    for (int i = 0, j = 0; i < n; i++) {
        while (j > 0 && text[ i] != pattern[j]) {
            j = next[j - 1];
        }
        if (text[ i] == pattern[j]) {
            j++;
        }
        if (j == m) {
            cnt++;
            j = next[j - 1];
        }
    }
    return cnt;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[ i];
    }
    for (int i = 0; i < n; i++) {
        int cnt_ab = kmp(a[ i], "ab");
        int cnt_ba = kmp(a[ i], "ba");
        if (a[ i].size() == 1) {
            cout << a[ i] << endl;
        } else {
            if (cnt_ab != cnt_ba) {
                a[ i][0] = 'b';
                cout << a[ i] << endl;
            } else {
                cout << a[ i] << endl;
            }
        }
    }
}

这样修改后的代码使用了KMP算法来统计字符串中 "ab" 和 "ba" 的出现次数,大大提高了效率,避免了超时问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 00:57:43 | 显示全部楼层    本楼为最佳答案   
这段代码中出现超时的问题主要是在字符串查找部分的逻辑,导致时间复杂度较高。为了提高效率,可以对字符串查找部分进行优化,同时简化冗余的逻辑。

以下是对代码的修改建议:
#include <iostream>#include <string>using namespace std;int main() {    int n;    const int N = 101;    string a[N];    cin >> n;    for (int i = 0; i < n; i++) {        cin >> a[i];    }    for (int i = 0; i < n; i++) {        int cnt_ab = 0, cnt_ba = 0;        for (int j = 0; j < a[i].size() - 1; j++) {            if (a[i][j] == 'a' && a[i][j + 1] == 'b') {                cnt_ab++;            }            if (a[i][j] == 'b' && a[i][j + 1] == 'a') {                cnt_ba++;            }        }        if (a[i].size() == 1) {            cout << a[i] << endl;        } else {            if (cnt_ab != cnt_ba) {                a[i][0] = 'b';            }            cout << a[i] << endl;        }    }    return 0;}
这样修改后的代码应该能够避免超时问题,并且更加高效地统计子串出现的次数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 09:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表