标签:acm codeforces
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 cin, cout streams or the %I64dspecifier.
Sample Input
369727
2
123456789987654321
1
1
1
Hint
Notes to the samples
In the first sample Inna can get the following numbers: 369727?→?99727?→?9997, 369727?→?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 */
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:
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
1 2 DI
Poor Dima!
2 2 MA ID
Poor Inna!
5 5 DIMAD DIMAI DIMAM DDMAA AAMID
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 */
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:acm codeforces
原文地址:http://blog.csdn.net/dojintian/article/details/47377705