2141:2333(zznuoj)

2141:2333(zznuoj)

2141: 2333

時間限制: 1 Sec  內存限制: 128 MB
提交: 77  解決: 17
[提交] [狀態] [討論版] [命題人:admin]

題目描述

“別人總說我瓜,其實我一點也不瓜,大多數時候我都機智的一批“ 寶兒姐考察你一道很簡單的題目。給你一個數字串,你能判斷有多少個連續子串能整除3嗎?

輸入

多實例輸入,以EOF結尾,每行一個數字串(長度<=1e6)

 

輸出

每行一個數字串,表示能整除3的連續子串的個數

 

樣例輸入

2333

 

樣例輸出

6
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long ll;
const int maxn = 1010000;
using namespace std;
int n, m,res;
int a[maxn];
int main()
{
    string sr;
    while (cin >> sr)
    {
        int len = sr.length();
        for (int i = 0; i < len; i++) a[i + 1] = (a[i] + sr[i] - '0')%3;
        int num[3] = { 0 };
        num[0] = 1;
        ll ans = 0;
        for (int i = 1; i <= len; i++)
        {
            ans += num[a[i]];
            num[a[i]]++;
        }
        cout << ans << endl;
    }
    return 0;
}

  

 

 
 
posted @ 2018-08-08 09:44 zzuli風塵 閱讀(...) 評論(...) 編輯 收藏
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章