2025.5.10

今天晚上本来要去写atcoder周赛的,但是还是没有抵住诱惑,玩了一晚上游戏嘻嘻,在这里好好忏悔一下。。。

1.首字母大写_牛客题霸_牛客网

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

int main() {
    string line;
    while (getline(cin, line)) {
        for (int i = 0; i < line.size(); ++i) {
            if (isalpha(line[i]) && (i == 0 || !isalpha(line[i - 1]))) {
                // 当前是字母,前一个不是字母,说明是单词首字母
                if (islower(line[i])) {
                    line[i] = toupper(line[i]);
                }
            }
        }
        cout << line << endl;
    }
    return 0;
}

无数次的格式错误,搞得有一些崩溃,有大佬知道我这个上面的代码怎么修改吗,有点难住了,(虽然没用已知条件。。。

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

int main() {
    string str;
    while(getline(cin,str)){
        if(str[0]>='a'&&str[0]<='z'){
            str[0]-=32;
        }
        for(int i=1;i<str.size();i++){
            while(str[i]==' '||str[i]=='\t'||str[i]=='\r'||str[i]=='\n'){
                i++;
                if(str[i]>='a'&&str[i]<='z'){
                    str[i]-=32;
                    break;
                }
            }
        }
        cout<<str<<endl;
    }
}

看了别人写的才发现有点简单了这道题

2.浮点数加法_牛客题霸_牛客网

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

int main() {
    string s1, s2;
    while (cin >> s1 >> s2) {
        size_t f1 = s1.find(".");
        size_t f2 = s2.find(".");
        string st1, st2, str1, str2;
        if (f1 != string::npos) {
            str1 = s1.substr(0, f1);
            st1 = s1.substr(f1 + 1);
        } else {
            str1 = s1;
            st1 = "";
        }
        if (f2 != string::npos) {
            str2 = s2.substr(0, f2);
            st2 = s2.substr(f2 + 1);
        } else {
            str2 = s2;
            st2 = "";
        }
        if(st1.length()<st2.length()) swap(st1,st2);
        string res1="";
        string res2="";
        string res3="";
        int c=0;
        string t1=st1.substr(0,st2.length());
        string t2=st1.substr(st2.length(),st1.length()-st2.length());
        reverse(t1.begin(),t1.end());
        reverse(st2.begin(),st2.end());
        for(int i=0;i<st2.length();i++){
            c+=t1[i]-'0'+st2[i]-'0';
            res1+=c%10+'0';
            c/=10;
        }
        reverse(res1.begin(),res1.end());
        res1+=t2;
        if(str1.length()<str2.length()) swap(str1,str2);
        string t3=str1.substr(str1.length()-str2.length(),str2.length());
        string t4=str1.substr(0,str1.length()-str2.length());
        reverse(t3.begin(),t3.end());
        reverse(str2.begin(),str2.end());
        for(int i=0;i<str2.length();i++){
            c+=t3[i]-'0'+str2[i]-'0';
            res2+=c%10+'0';
            c/=10;
        }
        reverse(res2.begin(),res2.end());
        if(c<1){
            t4+=res2;
            cout<<t4<<"."<<res1<<endl;
        }
        else{
            int s=0;
            reverse(t4.begin(),t4.end());
            for(int i=0;i<t4.size();i++){
                c+=t4[i]-'0';
                res3+=c%10+'0';
                c/=10;
            }
            reverse(res3.begin(),res3.end());
            res3+=res2;
            if(c==1) res3.insert(res3.begin(),c);
            cout<<res3<<"."<<res1<<endl;
        }
    }

    return 0;
}

感觉是自己大一写过的题了,忘的是一干二净,自己最后整数部分处理的时候自认为有点画蛇添足了,多创建了一个vector数组,但是这样自己可以通俗易懂

整数部分和小数部分分开算,小数部分高位对齐,相加,整数部分低位对齐,进位单独处理就可以了

3.后缀子串排序_牛客题霸_牛客网

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

int main() {
    string str;
    cin>>str;
    vector<string> m;
    int n=str.length();
    reverse(str.begin(),str.end());
    while(n>0){
        string s="";
        for(int i=0;i<n;i++) s+=str[i];           
        reverse(s.begin(),s.end());
        m.push_back(s);
        n--;
    }
    sort(m.begin(),m.end());
    for(auto &p:m) cout<<p<<endl;
}

有点能写了,方法不是最优的,最优的采用大佬写的stl,比我的stl简便

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    string str;
    cin >> str;
    vector<string> toSort;
    for (int i = 0; i < str.size(); ++i) //分割子串
        toSort.push_back(str.substr(i));
    sort(toSort.begin(), toSort.end()); //默认字典序排序
    for (int i = 0; i < toSort.size(); ++i)
        cout << toSort[i] << endl;
    return 0;
}

(取自牛客网题解处用户烤肉__)题解 | #后缀子串排序#_牛客博客

4.String Matching_牛客题霸_牛客网

#include<bits/stdc++.h>
using namespace std;
int nextr[1000];
void getnext(string b){
    int n=b.length();
    int i=0;
    nextr[i]=-1;
    int j=nextr[i];
    while(i<n){
        if(j==-1||b[i]==b[j]){
            i++;
            j++;
            nextr[i]=j;
        }
        else{
            j=nextr[j];
        }
    }
}
int kmp(string a,string b){
    getnext(b);
    int n=a.size();
    int m=b.size(); 
    int i=0;
    int j=0;
    int count=0;
    while(i<n){
        if(j==-1||a[i]==b[j]){
            i++;
            j++;
        }
        else j=nextr[j];
        if(j==m){
            count++;
            j=nextr[j];
        }
    }
    return count;
}
int main() {
    string a,b;
    cin>>a>>b;
    cout<<kmp(a,b)<<endl;

}

KMP板子题

5.字符串匹配_牛客题霸_牛客网

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

vector<vector<char>> build_pattern(const string& pattern) {
    vector<vector<char>> res;
    for (int i = 0; i < pattern.size(); ++i) {
        if (pattern[i] == '[') {
            int j = i + 1;
            vector<char> options;
            while (j < pattern.size() && pattern[j] != ']') {
                options.push_back(tolower(pattern[j]));  // 统一转小写进行比较
                j++;
            }
            res.push_back(options);
            i = j;  
        } else {
            vector<char> tmp;
            tmp.push_back(tolower(pattern[i]));
            res.push_back(tmp);
        }
    }
    return res;
}

bool is_match(const string& target, const vector<vector<char>>& pattern_rules) {
    if (target.size() != pattern_rules.size()) {
        return false;
    }

    for (int i = 0; i < target.size(); ++i) {
        char c = tolower(target[i]);
        bool matched = false;
        for (char option : pattern_rules[i]) {
            if (c == option) {
                matched = true;
                break;
            }
        }
        if (!matched) {
            return false;
        }
    }
    return true;
}

int main() {
    string line;
    while (getline(cin, line)) {
        int n = stoi(line); 
        vector<string> strings(n);

        for (int i = 0; i < n; ++i) {
            getline(cin, strings[i]);
        }

        string pattern;
        getline(cin, pattern);

        vector<vector<char>> pattern_rules = build_pattern(pattern);

        for (int i = 0; i < n; ++i) {
            if (is_match(strings[i], pattern_rules)) {
                cout << i + 1 << " " << strings[i] << endl;
            }
        }
    }
    return 0;
}

我承认这道题我确实没有想到对于正则表达式的问题,可以构建vector<vector<char>>来存储数据,非常巧妙,判断[]的位置对字符串进行push_back。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值