字符串总结

· · 闲话

错题:

比(fei)较(chang)多

第一次考试

1.B4003 [GESP202406 三级] 移位

原本代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int n;
    cin >> n;
    for(int i=0;i<=25;i++){
        s[i]+=n;
    }
    cout << s;
    return 0;
}

这样会导致移位后没有循环(如Z移1位后不会变成A);

正确代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int n;
    cin >> n;
    n%=26;
    for(int i=0;i<=25;i++){
        s[i]+=n;
        if(s[i]>'Z')s[i]-=26;
    }
    cout << s;
    return 0;
}

2.P3741 小果的键盘

原本代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int n,ans=0,f=1;
    cin >> n >> s;
    if(n==1){
        cout << 1;
        return 0;
    }
    for(int i=0;i<n;i++){
        if(s[i]=='V' and s[i+1]=='K'){
            ans++;
            s[i]=' ';
            s[i+1]=' ';
        }
        else if(s[i]=='K' and s[i-1]=='K' and f==1){
            s[i+1]=='K';
            f=0;
            ans++;
        }
        else if(s[i]=='V' and s[i+1]=='V' and f==1){
            s[i]=='V';
            f=0;
            ans++;
        }
    }
    cout << ans;
    return 0;
}

只会考虑第一个VV或KK,并且代码本身也有问题(s[i+1]=='K';)

正确代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int n,maxn=0;
    cin >> n >> s;
    for(int i=0;i<n-1;i++){
        if(s[i]=='V' and s[i+1]=='K')maxn++;
    }
    for(int i=0;i<n;i++){
        if(s[i]=='V')s[i]='K';
        else s[i]='V';
        int cnt=0;
        for(int j=0;j<n-1;j++){
            if(s[j]=='V' and s[j+1]=='K')cnt++;
        }
        if(s[i]=='V')s[i]='K';
        else s[i]='V';
        maxn=max(maxn,cnt);
    }
    cout << maxn;
    return 0;
}

3.B3640 T3 句子反转

原本代码:

#include<bits/stdc++.h>
using namespace std;
string a[1010];
int main(){
    string s;
    int x=0;
    while(cin >> s){
        for(int i=0;i<s.size();i++){
            if(s[i]>='a' and s[i]<='z')s[i]-=32;
            else if(s[i]>='A' and s[i]<='Z')s[i]+=32;
        }
        for(int i=0;i<s.size();i++){
            if(s[i]>='0' and s[i]<='9'){
                reverse(s.begin(),s.end());
            }
        }
        a[x]=s;
        x++;
    }
    for(int i=x;i>=0;i--){
        cout << a[i] << ' ';
    }
    return 0;
}

由于此代码会反转s.size()次数字,并且i要等于x-1

正确代码:

#include<bits/stdc++.h>
using namespace std;
string a[1010];
int main(){
    string s;
    int x=0;
    while(cin >> s){
        for(int i=0;i<s.size();i++){
            if(s[i]>='a' and s[i]<='z')s[i]-=32;
            else if(s[i]>='A' and s[i]<='Z')s[i]+=32;
        }
         if(s[0]>='0' and s[0]<='9')reverse(s.begin(),s.end());
        a[x]=s;
        x++;
    }
    for(int i=x-1;i>=0;i--){
        cout << a[i] << ' ';
    }
    return 0;
}

4.P1321 单词覆盖还原

原本代码:

没写(不会)

正确代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin >> s;
    int ansb=0,ansg=0;
    for(int i=0;i+2<s.size();i++){
        if(s[i]=='b' || s[i+1]=='o' || s[i+2]=='y')ansb++;
    }
    for(int i=0;i+3<s.size();i++){
        if(s[i]=='g' || s[i+1]=='i' || s[i+2]=='r' || s[i+3]=='l')ansg++;
    }
    cout << ansb << endl << ansg;
    return 0;
}

5.P1125 [NOIP 2008 提高组] 笨小猴

原本代码:

依旧没写(不会)

正确代码:

#include<bits/stdc++.h>
using namespace std;
int a[100010];
bool yuan(int a){
    if(a==0 or a==1){
        return 0;
    }
    int ans=0;
    for(int i=2;i<a;i++){
        if(a%i==0){
            return 0;
        }
    }
    return 1;
}
int main(){
    string s;
    cin >> s;
    int maxn=0,minn=1000;
    for(int i=0;i<s.size();i++){
        a[s[i]-96]++;
    }
    for(int i=1;i<110;i++){
        maxn=max(maxn,a[i]);
        if(a[i]!=0){
            minn=min(minn,a[i]);
        }
    }
    if(yuan(maxn-minn)==1){
        cout << "Lucky Word" << endl << maxn-minn;
    }
    else cout << "No Answer" << endl << 0;
    return 0;
}

第二次考试

由于除了第一题都没写,所以直接展示代码:

B3920 [语言月赛 202401] Genshin 玩家:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin >> s;
    int ans=0,pos=s.find("Genshin",0);
    while(pos!=string::npos){
        for(int j=pos+1;j+5<s.size();j++){
            if(s.substr(j,6)=="player")ans++;
        }
        pos=s.find("Genshin",pos+1);
    }
    cout << ans;
    return 0;
}

P8835 [传智杯 #3 决赛] 子串:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin >> T;
    while(T--){
        int n,m,ans=0;
        string s,w;
        cin >> n >> m >> s >> w;
        for(int i=0;i<s.size();i++)if(s[i]>='A' and s[i]<='Z')s[i]+=32;
        for(int i=0;i<w.size();i++)if(w[i]>='A' and w[i]<='Z')w[i]+=32;
        int pos=w.find(s,0);
        while(pos!=string::npos){
            ans++;
            pos=w.find(s,pos+1);
        }
        cout << ans << endl;
    }
    return 0;
}

B3990 [语言月赛 202406] 通配符匹配:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s,t;
    cin >> s >> t;
    for(int i=0;i+t.size()-1<s.size();i++){
        string st=s.substr(i,t.size());
        int f=0;
        for(int j=0;j<st.size();j++){
            if(st[j]!=t[j] and st[j]!='?' and t[j]!='?'){
                f=1;
                break;
            }
        }
        if(f==0){
            cout << i+1 << ' ' << i+t.size() << endl;
        }
    }
    return 0;
}

B4499 [GESP202603 三级] 二进制回文串:

#include<bits/stdc++.h>
using namespace std;
bool yuan(long long x){
    string ans;
    while(x!=0){
        int v=x%2;
        ans=char(v+'0')+ans;
        x/=2;
    }
    string s=ans;
    reverse(s.begin(),s.end());
    if(s==ans)return 1;
    else return 0;
}
int main(){
    int n,ans=0;
    cin >> n;
    for(int i=1;i<=n;i++){
        if(yuan(i))ans++;
    }
    cout << ans;
    return 0;
}

第三次考试

B3891 [语言月赛 202311] 基因

原本代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin >> T;
    while(T--){
        string s;
        int n,ans=0,f=1;
        cin >> n >> s;
        for(int i=0;i<n;i++){
            if(s[i]!='A' and s[i]!='T' and s[i]!='G' and s[i]!='C'){
                cout << 0;
                f=0;
                break;
            }
        }
        if(f==0)continue;
        string s1=s;
        reverse(s1.begin(),s1.end());
        for(int i=0;i<n;i++){
            if((s[i]=='A' and s1[i]=='T') or (s[i]=='G' and s1[i]=='C') or (s[i]=='T' and s1[i]=='A') or (s[i]=='C' and s1[i]=='G')){
                ans+=i+1;
            }

        }
        cout << ans << endl;
    }
    return 0;
} 

int太小了(真)

正确代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin >> T;
    while(T--){
        string s;
        int n;
        long long ans=0,f=1;
        cin >> n >> s;
        for(int i=0;i<n;i++){
            if(s[i]!='A' and s[i]!='T' and s[i]!='G' and s[i]!='C'){
                cout << 0 << endl;
                f=0;
                break;
            }
        }
        if(f==0)continue;
        string s1=s;
        reverse(s1.begin(),s1.end());
        for(int i=0;i<n;i++){
            if((s[i]=='A' and s1[i]=='T') or (s[i]=='G' and s1[i]=='C') or (s[i]=='T' and s1[i]=='A') or (s[i]=='C' and s1[i]=='G')){
                ans+=i+1;
            }

        }
        cout << ans << endl;
    }
    return 0;
} 

P8871 [传智杯 #5 初赛] C-莲子的排版设计学

原本代码:

没写

正确代码:

#include<bits/stdc++.h>
using namespace std;
string str[20010];
int main(){
    string s;
    int n=0;
    while(getline(cin,s)){
        n++;
        str[n]=s;

    }
    int m=n,ans=0;
    while(m!=0){
        m/=10;
        ans++;
    }
    for(int i=1;i<=n;i++){
        cout << setw(ans) << i << " " << str[i] << endl;
    }
    return 0;
} 

T794575 始终-hard

原本代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    long long ans=0;
    cin >> s;
    for(int i=0;i<s.size();i++){
        for(int j=i;j<s.size();j++){
            string s1=s.substr(i,j-i+1);
            if(s1[0]==s1[s1.size()-1])ans++;
        }
    }
    cout << ans;
    return 0;
} 

这样会超时

正确代码:

#include<bits/stdc++.h>
using namespace std;
int a[100];
int main(){
    string s;
    long long ans=0;
    cin >> s;
    for(long long i=0;i<s.size();i++){
        a[s[i]-'a']++;
        ans+=a[s[i]-'a'];
    }
    cout << ans;
    return 0;
} 

U556432 暴力枚举

原本代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,ans=0;
    cin >> n;
    for(long long i=1;i<=n;i++){
        for(long long j=1;j*i<=n;j++){
            for(long long k=1;k*i*j<=n;k++){
                if(i<=j and j<=k and i*j*k<=n){
                    ans++;
                }
            }
        }
    }
    cout << ans;
    return 0;
} 

这样也会超时

正确代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,ans=0;
    cin >> n;
    for(long long i=1;i*i*i<=n;i++){
        for(long long j=i;i*j*j<=n;j++){
            long long k=n/i/j;
            if(k>=j)ans+=(k-j+1);
        }
    }
    cout << ans;
    return 0;
} 

字符串函数:

reverse函数:
可以将字符串反转
有两个量,分别是字符串的头与字符串的尾+1;
也就是begin()与end();
代码:reverse(s.bigen(),s.end());
注:reverse函数的返回值是直接输入进原字符串的。
find函数:
在字符串中查找子串
至少有一个量,表示要查找的子串是什么;
第二个量表示从哪一位开始查找;
如果没有就默认为从开头开始;
代码:s.find("****")或s.find("****",i);
注:查找到了返回第一个查找的字符的下标,没有则返回string::npon。
substr函数:
在字符串中截取子串
也是至少一个量,表示从哪个字符开始截取;
第二个量表示截取的长度;
如果没有就默认为截取到字符串末尾;
代码:s.substr(l,r-l+1)或s.substr(r);
注:第二个量是长度,不是末尾