c++中字符串最大扭矩的问题

有的答案正确,有的答案超时,但是答案都不对,不超时的那个 我是借鉴了一下输入样例,样例中有两个ef,答案应该是第二个ef和ab的跨距,而我的代码的答案是第一个ef与ab的跨距,不知道哪里不对,我单独输出了一下t1和t2,t1总是对的,t2都不对,而且单纯输出t2还有可能超时,所以应该是包含t2循环的错误,但是我找不出来,不要新写的代码,想知道错误在哪,有没有会debug的,可以弄一下,我不会

img

img

img


#include 
#include 
#include 
#include 

using namespace std;

int main(){
    string s,s1,s2,s3;
    int x;
    cin >> s;
    int z=s.size();
    for(int i=0;i
        if(s[i]==',') break;
        s1=s1+s[i];
        x=i;
    }
    for(int i=x+2;i
            if(s[i]==',') break;
            s2=s2+s[i];
            x=i;
    }
    for(int i=x+2;i
        s3=s3+s[i];                     //上面三个for循环的作用是把三个字符串分别提出来,已经测试过没有问题
    }
        int z1=s1.size(),z2=s2.size(),z3=s3.size();
        int t1=0,flag1=0;
    for(int i=0,j=0,count=0;i
        if(s1[i]==s2[j%z2]){
        count++;
        }
        if(count==z2){
        flag1=1;
        t1=i;                               //这个for循环的作用是找出s1在s中的第一个位置并将其最后一个字母在s中的坐标赋给t1
        break;
        }
        if (s1[i]!=s2[j]){
        i=i-count;
        count=0;
        j=j-count-1;
        }
    }                                       
        int t2=0,flag2=0;
     for(int i=0,j=0,count=0;i
        if(s1[i]==s3[j%z3]) 
        count++;
        if(count==z3){
        flag2=1;
        t2=i-count+1;
        i=i-count+1;
        j=-1;
        }                           //这个for循环的作用是找出s2在s中的最后一个位置并将其最后一个位置的首字母在s中的坐标赋给t2
        if(s1[i]!=s3[j]){           //因为要找最后一个s2在s中出现的位置,所以即使找到了相匹配的字符,我还是把坐标减到匹配之前的位置上
        i=i-count;                  //然后结尾的时候i会自增1,所以相当于从匹配的字符的首字母的坐标的下一个坐标继续开始匹配,直到达到z1
        count=0;
        j=j-count-1;
        }
    }
       int length=t2-t1-1;
    if(flag1==0 || flag2==0 || length<0)
    cout << "-1";
    else 
    cout << length;
}

同一个题啊,刚改完代码

img

代码修改如下:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>

using namespace std;

int main() {
    string s, s1="", s2="", s3="";
    int x;
    cin >> s;
    int z = s.size();
    for (int i = 0; i < z; i++) {
        if (s[i] == ',') break;
        s1 = s1 + s[i];
        x = i;
    }
    for (int i = x + 2; i < z; i++) {
        if (s[i] == ',') break;
        s2 = s2 + s[i];
        x = i;
    }
    for (int i = x + 2; i < z; i++) {
        s3 = s3 + s[i];
    }
    int z1 = s1.size(), z2 = s2.size(), z3 = s3.size();  //修改
    int t1 = 0, flag1 = 0;
    for (int i = 0, j = 0, count = 0; i < z1 && j<z2; i++, j++) { //修改
        if (s1[i] == s2[j]) { //修改
            count++;
        }
        if (count == z2) {
            flag1 = 1;
            t1 = i;
            break;
        }
        if (s1[i] != s2[j]) {  //修改  s2从头开始
            //i = i - count;
            count = 0;
            //j = j - count - 1;
            j = -1;
        }
    }
    //s3应该从s1的最右侧开始判断
    int t2 = 0, flag2 = 0;
    int tmp = z1-z3;
    for (int i = z1-z3, j = 0, count = 0; i >= 0 && j < z3; i++, j++) { //修改
        
        if (s1[i] == s3[j])
            count++;
        if (count == z3) {
            flag2 = 1;
            t2 = i - count + 1;
            break;
            //i = i - (count - 1);
            //count = 0;
        }
        if (s1[i] != s3[j]) {
            //i = i - count;
            tmp--; //tmp前移
            i = tmp - 1; //i 前移
            count = 0;
            //j = j - count - 1;
            j = -1;
        }
    }
    if (flag1 == 1 && flag2 == 1 && t2 > t1)
    {
        int length = t2 - t1 - 1;
        cout << length;
    }
    else
        cout << "-1";
    return 0;
}


既然用到string,那么字符串子串搜索完全可以用string类的函数find和rfind搜索啊,为什麽要逐个字符去匹配呢?

48行if里没有吧count重置为0

你把问题想的太复杂了,s1从前面开始查找,s2从后面开始查找,然后判断一下就行了。

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> split(const std::string &str, char delimiter = ',') {
  std::vector<std::string> strs;
  std::size_t last = 0, next = 0;
  while ((next = str.find(delimiter, last)) != std::string::npos) {
    strs.push_back(str.substr(last, next - last));
    last = next + 1;
  }
  strs.push_back(str.substr(last));
  return strs;
}

int main() {
  std::string line;
  std::cin >> line;
  auto s = split(line);
  auto pos1 = s[0].find(s[1]);
  auto pos2 = s[0].rfind(s[2]);
  if (pos1 == std::string::npos || pos2 == std::string::npos ||
      pos1 + s[1].size() > pos2)
    std::cout << -1 << '\n';
  else
    std::cout << pos2 - pos1 - s[1].size() << '\n';
  return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632