ZOJ 3983简单模拟

C++字符串处理算法
本文介绍了一种使用C++处理特定字符串模式的问题解决方法。该算法通过检查输入字符串中的重复字符序列来确定需要进行的操作,并根据不同的条件计算出操作数。文章详细展示了如何通过两轮检查来高效地处理字符串,并提供了完整的代码实现。
#include <bits/stdc++.h>

using namespace std;


int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while(T --) {
        string s, t;
        cin >> s;
        int res = 0;
        for(int i = 0;i < 7;i ++) {
            if(s[i] == s[i+1] && s[i+1] == s[i+2]) {
                res ++;
                for(int j = 0;j < i;j ++) t = t +  s[j];
                for(int j = i + 3;j < 9;j ++) t = t + s[j];
                break;
            }
        }
        if(res) {
//            cout <<t << endl;
            s = t;
            t.clear();
            for(int i = 0;i < 4;i ++) {
                if(s[i] == s[i + 1] && s[i + 1] == s[i + 2]) {
                    res ++;
                    for(int j = 0;j < i;j ++) t = t + s[j];
                    for(int j = i + 3;j < 6;j ++) t = t + s[j];
                    break;
                }
            }
            if(res == 2) {
                if(t[0]==t[1]&&t[1]==t[2]) cout << 3 << '\n';
                else cout << 2 << '\n';
            } else {
                map<char,int> mp;
                for(int i = 0;i < 6;i ++) mp[s[i]] ++;
                for(map<char,int>::iterator it = mp.begin() ;it!=mp.end();it++) {
                    if((*it).second > 2) {
                        res ++;
                        break;
                    }
                }
                cout << res << '\n';
            }
        } else {
            int res = 1;
            for(int l = 0;l < 9;l ++) {
                for(int r = l;r < 9;r ++) {
                    string tmp1, tmp2;
                    for(int i = l;i <= r;i ++) tmp1 = tmp1 + s[i];
                    for(int i = 0;i < l;i ++) tmp2 = tmp2 + s[i];
                    for(int i = r + 1;i < 9;i ++) tmp2 = tmp2 + s[i];
//                    cout << tmp1 << " " << tmp2 << endl;
                    int t1=0,t2=0;
                    map<char,int> mp1,mp2;
                    for(int i = 0;i < tmp1.size();i ++) mp1[tmp1[i]] ++;
                    for(int i = 0;i < tmp2.size();i ++) mp2[tmp2[i]] ++;
                    for(map<char,int>::iterator it = mp1.begin() ;it!=mp1.end();it++) {
                        if((*it).second > 2) {
                            t1 = 1;
                            break;
                        }
                    }
                    for(map<char,int>::iterator it = mp2.begin() ;it!=mp2.end();it++) {
                        if((*it).second > 2) {
                            t2 = 1;
                            break;
                        }
                    }
                    res = max(res, t1 + t2);
                }
            }
            cout << res << '\n';
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值