2 条题解

  • 0
    @ 2025-7-26 20:43:44

    666

    #include<bits/stdc++.h>
    using namespace std;
    int main() {
    	/*ios::sync_with_stdio(false);
    	cin.tie(0); cout.tie(0);*/
    	string a;
    	bool b['z'+1]={false};
    	cin>>a;
    	int l=0,r=1,x=1;
    	b[a[0]]=true;
    	while(r<a.size()){
    		if(b[a[r]]==false){
    			b[a[r]]=true;
    			x=max(r++-l,x);
    		}
    		else{
    			b[a[l]]=false;
    			x=max(r-l++,x);
    		}
    	}
    	cout<<x;
    	return 0;
    }
    
    • 0

      滑动窗口,建左右两个指针、一个用于判断是否重复的map

      遍历左指针,若不为00,则把最左边的字符s[l]对应的值标记为00

      接下来右指针开始向右移,有重复的字符Hash[s[r+1]]==1或者越界停止,每次把最右边的字符标记为11

      然后rl+1r-l+1就是一个符合条件的子串长度,记录下来打擂台即可。

      #include<bits/stdc++.h>
      using namespace std;
      string s;
      map<char,bool> Hash;
      int ans=1;
      int main(){
      	cin>>s;
      	int n=s.size();
      	int l=0,r=-1;
      	for(l;l<n;l++){
      		if(l>0){
      			Hash[s[l-1]]=0;
      		}
      		while(r+1<n&&Hash[s[r+1]]==0){
      			r++;
      			Hash[s[r]]=1;
      		}
      		ans=max(ans,r-l+1);
      	}
      	cout<<ans;
      }
      
      • 1

      信息

      ID
      911
      时间
      1000ms
      内存
      256MiB
      难度
      8
      标签
      (无)
      递交数
      32
      已通过
      6
      上传者