[蓝桥杯2019初赛]不同子串-substr,模拟
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                [蓝桥杯2019初赛]不同子串-substr,模拟
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                題目描述
 一個(gè)字符串的非空子串是指字符串中長(zhǎng)度至少為1 的連續(xù)的一段字符組成的串。
 例如,字符串a(chǎn)aab 有非空子串a(chǎn), b, aa, ab, aaa, aab, aaab,一共7 個(gè)。
 注意在計(jì)算時(shí),只算本質(zhì)不同的串的個(gè)數(shù)。
 請(qǐng)問,字符串0100110001010001 有多少個(gè)不同的非空子串?
代碼如下:
#include <iostream> #include <cstring> #include <map> using namespace std; map<string, int>st;int main() {int cnt = 0;string a = "0100110001010001";int len = a.length();for (int i = 0; i < len; i++) {for (int j = 1; j <= len; j++) {string ss = a.substr(i, j);if (st.count(ss) == 0) {st[ss] = 1;cnt++;}}}cout << cnt << endl;return 0; }為什么下面的代碼ac不了呢???
#include <iostream> #include <cstring> #include <map> using namespace std; map<string, int>st;int main() {int cnt = 0;string a = "0100110001010001";int len = a.length();for (int i = 0; i < len; i++) {for (int j = 1; j < len; j++) {string ss = a.substr(i, j);if (st.count(ss) == 0) {st[ss] = 1;cnt++;}}}cout << cnt << endl;return 0; }相信你看完這個(gè)就明白了!!!
總結(jié)
以上是生活随笔為你收集整理的[蓝桥杯2019初赛]不同子串-substr,模拟的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: [蓝桥杯]回形取数-方向向量+模拟
- 下一篇: 联想睿影620S台式机电脑评测联想台式电
