codeforces

12 篇文章 0 订阅
B - Inna and Nine
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number a, consisting of digits from 1 to 9.

Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine as possible. In one move, Inna can choose two adjacent digits in a number which sum equals 9 and replace them by a single digit 9.

For instance, Inna can alter number 14545181 like this: 14545181 → 1945181 → 194519 → 19919. Also, she can use this method to transform number 14545181 into number 19991. Inna will not transform it into 149591 as she can get numbers 19919and 19991 which contain more digits nine.

Dima is a programmer so he wants to find out how many distinct numbers containing as many digits nine as possible Inna can get from the written number. Help him with this challenging task.

Input

The first line of the input contains integer a(1 ≤ a ≤ 10100000). Number a doesn't have any zeroes.

Output

In a single line print a single integer — the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed 263 - 1.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Sample Input

Input
369727
Output
2
Input
123456789987654321
Output
1
Input
1
Output
1

Hint

Notes to the samples

In the first sample Inna can get the following numbers: 369727 → 99727 → 9997369727 → 99727 → 9979.

In the second sample, Inna can act like this: 123456789987654321 → 12396789987654321 → 1239678998769321.

#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
typedef pair<int , int> pii;
#define N 100000 + 10

int cnt[N];
char s[N];

int main()
{
    while(~scanf("%s", s))
    {
        int len = strlen(s);
        LL sum = 1;
        fill(cnt, cnt + len + 1, 1);
        for(int i = 1; i <= len; i++)
        {
            if((s[i]-'0') + (s[i-1]-'0') == 9)
                cnt[i] = cnt[i-1] + 1;
            else if(cnt[i-1] > 2 && (cnt[i-1] & 1)) sum *= (cnt[i-1]/2 + 1);
        }
        printf("%I64d\n", sum);
    }
    return 0;
}

/*
123456789987654321
369727
*/

C - Inna and Dima
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Inna and Dima bought a table of size n × m in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A".

Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows:

  1. initially, Inna chooses some cell of the table where letter "D" is written;
  2. then Inna can move to some side-adjacent table cell that contains letter "I"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter "M"; then she can go to a side-adjacent cell that contains letter "A". Then Inna assumes that she has gone through her sweetheart's name;
  3. Inna's next move can be going to one of the side-adjacent table cells that contains letter "D" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she always goes to the letter "I", from the letter "I" she always goes the to letter "M", from the letter "M" she always goes to the letter "A", and from the letter "A" she always goes to the letter "D".

Depending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA.

Input

The first line of the input contains two integers n and m(1 ≤ n, m ≤ 103).

Then follow n lines that describe Inna and Dima's table. Each line contains m characters. Each character is one of the following four characters: "D", "I", "M", "A".

Note that it is not guaranteed that the table contains at least one letter "D".

Output

If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA.

Sample Input

Input
1 2
DI
Output
Poor Dima!
Input
2 2
MA
ID
Output
Poor Inna!
Input
5 5
DIMAD
DIMAI
DIMAM
DDMAA
AAMID
Output
4

Hint

Notes to the samples:

In the first test sample, Inna cannot go through name DIMA a single time.

In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner.

In the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times.


 
<pre name="code" class="cpp">//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
typedef pair<int , int> pii;

#define maxn 1000 + 10

char s[maxn][maxn];
int vis[maxn][maxn];
int d[maxn][maxn];
int n, m;
int go[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
map<char, char> M;

int ans = 0;

int dfs(int x, int y, char ch)
{
    //cout<<x<<y<<endl;
    if(d[x][y] != -1) return d[x][y];
    int res = 0;
    vis[x][y] = 1;
    for(int i = 0; i < 4; i++)
    {
        int dx = x + go[i][0];
        int dy = y + go[i][1];

        if(dx > n || dx <= 0 || dy > m || dy <= 0 || M[ch] != s[dx][dy]) continue;
        if(vis[dx][dy])
            res = INF;
        else
        {
            int tmp = dfs(dx, dy, s[dx][dy]);
            res = max(res, tmp);
        }
    }
    res += 1;
    vis[x][y] = 0;
    return d[x][y] = res;
}

int main()
{
    M['D'] = 'I';
    M['I'] = 'M';
    M['M'] = 'A';
    M['A'] = 'D';

    memset(d, -1, sizeof d);
    memset(vis, 0, sizeof vis);

    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%s", s[i] + 1);

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            if(s[i][j] == 'D' && d[i][j] == -1)
               {
                   int t = dfs(i, j, 'D');
                   ans = max(ans, t / 4);
               }
        }
    if(!ans) printf("Poor Dima!\n");
    else if(ans >= INF/4) printf("Poor Inna!\n");
    else printf("%d\n", ans);

    return 0;
}


/*

5 5
DIMAD
DIMAI
DIMAM
DDMAA
AAMID


*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值