winaichan263's blog

By winaichan263, history, 9 years ago, In English

Is there something wrong with this?

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

int main(){
    string s;
    cin >> s;
    vector<int> bears;
	int len = s.length();
    for(int i = 0; i < len; i++){
        if(s[i] == 'b' && i+3 < len){
            if(s[i+1] == 'e' && s[i+2] == 'a' && s[i+3] == 'r') bears.push_back(i);
        }
    }
    int bi = 0;
    long long count = 0;
    for(int i = 0; i <= bears[bears.size()-1]; i++){
        if(i <= bears[bi]) count += len-(bears[bi]+3);
        else{
            if(bi < bears.size()-1) count += len-(bears[++bi]+3);
        }
    }
    cout << count;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The problem is in the line

for(int i = 0; i <= bears[bears.size() — 1]; i++){

What if bears.size() is 0?