codeforces1833A

1. 题目链接

Musical Puzzle

2. 题目代码

正确代码

#include<iostream>
#include<vector>
#include<set>
using namespace std;
int main(){
    int testCase;
    cin >> testCase;
    int lengthOfMelody;
    string melody;
    set<string> twoNote;
    string note;
    while(testCase --){
        cin >> lengthOfMelody;
        cin >> melody;
        for(int subscript = 0; subscript < lengthOfMelody; subscript ++){
            note += melody[subscript];
            if(note.length() == 2){
                twoNote.insert(note);
                note = "";
                note += melody[subscript];
            }
        }
        cout << twoNote.size() << endl;
        twoNote.clear();
        note = "";
    }
    return 0;
}

问题代码(包含代码调试部分,可忽略)

#include<iostream>
#include<vector>
#include<set>
using namespace std;
int main(){
    int testCase;
    cin >> testCase;
    int lengthOfMelody;
    string melody;
    set<string> twoNote;
    string note;
    while(testCase --){
        cin >> lengthOfMelody;
        cin >> melody;
        for(int subscript = 0; subscript < lengthOfMelody; subscript ++){
            note += melody[subscript];
            //cout << "note=" << note << endl;
            if(note.length() == 2){
                twoNote.insert(note);
                cout << "note=" << note << endl;
                note = "";
                note += melody[subscript];
            }
            // if(subscript != 0 && subscript != lengthOfMelody - 1){
            //     if(melody[subscript - 1] == melody[subscript + 1]){
            //         if(note == ""){
            //             note += melody[subscript];
            //         }
            //         note += melody[subscript + 1];
            //         twoNote.insert(note);
            //         cout << "note=" << note << endl;
            //         note = "";
            //         note += melody[subscript + 1];
            //         note += melody[subscript];
            //         twoNote.insert(note);
            //         cout << "note=" << note << endl;
            //         note = "";
            //         subscript ++;
            //     }
            // }
        }
        cout << twoNote.size() << endl;
        twoNote.clear();
        note = "";
        cout << "check=" << twoNote.size() << endl;
    }
    return 0;
}

3. 题目总结

在这里插入图片描述
① 这个题目我解了将近96分钟。中间洋洋洒洒写了那么多,后来发现有没有都一样🤣
② 上述问题代码只记录了我中后阶段的解题思路,最初的思路错的太离谱了,我在写的过程就删掉了。正确代码for循环里的note += melody[subscript];也是在调试过程中发现问题后加上去的(可以用7 abcdefg这个样例检测该行代码的作用)。
③ 一开始我采用的是vector容器,并且引入变量miniNumber记录输出结果,但是我后来发现这种方式无法排除重复短字符串的情况,因此我后来改成了set容器。
④ note += melody[subscript]; note += melody[subscript + 1]; 并不等同于
note = melody[subscript] + melody[subscript + 1]; 后者会得到空字符串
⑤ 看了一眼题解,发现我好像把这个题想复杂了。其实就是按顺序将字符串两两一组,输出组中字符串不同的小组个数

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值