C++,codeforces,2048C,C. Kevin and Binary Strings

//2048C
/*
codeforces,2048C,C. Kevin and Binary Strings
https://codeforces.com/problemset/problem/2048/C
*/
#include <iostream>
#include <algorithm>
void solve(){
    std::string str;
    std::cin>>str;
    int l1 = 0,r1 = 0,l2=1,r2=str.size();
    int secondSubstrLen;
    size_t zeroPos = str.find('0');
    if(zeroPos == std::string::npos){
        l1 = 1;r1 = 1;
        std::cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<std::endl;
        return;
    }
    secondSubstrLen = str.size() - zeroPos;
    std::string xorStr = str.substr(zeroPos);
    for_each(xorStr.begin(),xorStr.end(),[](char& a){a = (a-'0')^1+'0';});
    int matchLength = 0;
    for(int i = 0;i+secondSubstrLen<=str.size();++i){
        if(str[i]==xorStr[0]){
            int tempMatchLength = 0;
            int j = 1;
            for(;j<secondSubstrLen;++j){
                if(str[i+j]!=xorStr[j]){
                    tempMatchLength = j;
                    if(tempMatchLength>matchLength){
                        l1 = i+1;
                        matchLength = tempMatchLength;
                    }
                    break;
                }
            }
            if(j==secondSubstrLen){
                matchLength = secondSubstrLen;
                l1 = i+1;
                break;
            }
        }
    }
    r1 = l1+secondSubstrLen-1;
    std::cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<std::endl;
}
int main(){
    int t;
    std::cin>>t;
    while(t--){
        solve();
    }
}


/*
#include <iostream>
#include <string>
int main(){
    std::string str = "abcde";
    size_t aPos = str.find('a');
    std::cout<<aPos<<std::endl;//0
}
*/



/*
codeforces,2048C,C. Kevin and Binary Strings
https://codeforces.com/problemset/problem/2048/C
*/
#include <iostream>
#include <string>
using namespace std;
int main(){
    int t; cin>>t;
    while(t--){
        string s;
        cin>>s;
        int one=0, ze=0, idx=-1;//one是字符串开头1的个数, ze是字符串开头0的个数, idx是字符串中第一个0的下标
        for(int i=0; i<s.size(); i++){
            if(s[i]=='1') one++;
            else {
                idx=i;
                break;
            }
        }
        for(int i=idx; i<s.size(); i++){
            if(s[i]=='0') ze++;
            else break;
        }
        cout<<1<<" "<<s.size()<<" ";
        if(one==s.size()) cout<<1<<" "<<1;
        else if(one>ze) cout<<idx-ze+1<<" "<<s.size()-ze;
        else cout<<1<<" "<<s.size()-one;
        cout<<'\n';
    }
}
posted @ 2025-03-07 19:59  Kazuma_124  阅读(27)  评论(0)    收藏  举报