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;
}

  

 

 
 
原文地址:https://www.cnblogs.com/kangdong/p/9440967.html