鱼C论坛

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

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

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

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

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

x
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. const int N=101;
  5. string a[N];
  6. int main()
  7. {
  8.     cin>>n;
  9.     string s;
  10.     for(int i=0;i<n;i++)
  11.     {
  12.         cin>>a[i];
  13.     }
  14.     for(int i=0;i<n;i++)
  15.     {
  16.         int pos=-1;
  17.         int cnt=0,ans=0;
  18.         string c="ab";
  19.         string d="ba";
  20.         while((pos=a[i].find(c,pos+1))!=-1)
  21.         {
  22.             cnt++; //
  23.         }
  24.         while ((pos=a[i].find(d,pos+1))!=-1)
  25.         {
  26.             ans++;
  27.         }
  28.    
  29.         if(a[i].size()==1)cout<<a[i]<<endl;
  30.         else
  31.         {
  32.             if(cnt!=ans)
  33.             {
  34.                 a[i][0]='b';
  35.                 cout<<a[i]<<endl;
  36.             }
  37.             else cout<<a[i]<<endl;
  38.          
  39.             

  40.         }
  41.         
  42.         
  43.     }
  44.    



  45. }
复制代码

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

以下是对代码的修改建议:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5.     int n;
  6.     const int N = 101;
  7.     string a[N];
  8.     cin >> n;
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> a[i];
  11.     }
  12.     for (int i = 0; i < n; i++) {
  13.         int cnt_ab = 0, cnt_ba = 0;
  14.         for (int j = 0; j < a[i].size() - 1; j++) {
  15.             if (a[i][j] == 'a' && a[i][j + 1] == 'b') {
  16.                 cnt_ab++;
  17.             }
  18.             if (a[i][j] == 'b' && a[i][j + 1] == 'a') {
  19.                 cnt_ba++;
  20.             }
  21.         }
  22.         if (a[i].size() == 1) {
  23.             cout << a[i] << endl;
  24.         } else {
  25.             if (cnt_ab != cnt_ba) {
  26.                 a[i][0] = 'b';
  27.             }
  28.             cout << a[i] << endl;
  29.         }
  30.     }
  31.     return 0;
  32. }
复制代码

这样修改后的代码应该能够避免超时问题,并且更加高效地统计子串出现的次数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

下面是修改后的代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. int n;
  4. const int N=101;
  5. string a[N];

  6. // KMP算法中的next数组计算函数
  7. vector<int> getNext(string pattern) {
  8.     int m = pattern.size();
  9.     vector<int> next(m, 0);
  10.     for (int i = 1, j = 0; i < m; i++) {
  11.         while (j > 0 && pattern[ i] != pattern[j]) {
  12.             j = next[j - 1];
  13.         }
  14.         if (pattern[ i] == pattern[j]) {
  15.             j++;
  16.         }
  17.         next[ i] = j;
  18.     }
  19.     return next;
  20. }

  21. // KMP算法
  22. int kmp(string text, string pattern) {
  23.     int n = text.size(), m = pattern.size();
  24.     vector<int> next = getNext(pattern);
  25.     int cnt = 0;
  26.     for (int i = 0, j = 0; i < n; i++) {
  27.         while (j > 0 && text[ i] != pattern[j]) {
  28.             j = next[j - 1];
  29.         }
  30.         if (text[ i] == pattern[j]) {
  31.             j++;
  32.         }
  33.         if (j == m) {
  34.             cnt++;
  35.             j = next[j - 1];
  36.         }
  37.     }
  38.     return cnt;
  39. }

  40. int main() {
  41.     cin >> n;
  42.     for (int i = 0; i < n; i++) {
  43.         cin >> a[ i];
  44.     }
  45.     for (int i = 0; i < n; i++) {
  46.         int cnt_ab = kmp(a[ i], "ab");
  47.         int cnt_ba = kmp(a[ i], "ba");
  48.         if (a[ i].size() == 1) {
  49.             cout << a[ i] << endl;
  50.         } else {
  51.             if (cnt_ab != cnt_ba) {
  52.                 a[ i][0] = 'b';
  53.                 cout << a[ i] << endl;
  54.             } else {
  55.                 cout << a[ i] << endl;
  56.             }
  57.         }
  58.     }
  59. }
复制代码


这样修改后的代码使用了KMP算法来统计字符串中 "ab" 和 "ba" 的出现次数,大大提高了效率,避免了超时问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

以下是对代码的修改建议:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5.     int n;
  6.     const int N = 101;
  7.     string a[N];
  8.     cin >> n;
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> a[i];
  11.     }
  12.     for (int i = 0; i < n; i++) {
  13.         int cnt_ab = 0, cnt_ba = 0;
  14.         for (int j = 0; j < a[i].size() - 1; j++) {
  15.             if (a[i][j] == 'a' && a[i][j + 1] == 'b') {
  16.                 cnt_ab++;
  17.             }
  18.             if (a[i][j] == 'b' && a[i][j + 1] == 'a') {
  19.                 cnt_ba++;
  20.             }
  21.         }
  22.         if (a[i].size() == 1) {
  23.             cout << a[i] << endl;
  24.         } else {
  25.             if (cnt_ab != cnt_ba) {
  26.                 a[i][0] = 'b';
  27.             }
  28.             cout << a[i] << endl;
  29.         }
  30.     }
  31.     return 0;
  32. }
复制代码

这样修改后的代码应该能够避免超时问题,并且更加高效地统计子串出现的次数。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 15:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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