zcmu1038
2018-03-07 21:32
197 查看
1038: 二哥的困惑 Ⅰ
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 107 Solved: 56
[Submit][Status][Web Board]
Description
There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts.Current character pointer (CP);Direction pointer (DP) which can point left or right;
Initially CP points to the leftmost character of the sequence and DP points to the right.We repeat the following steps until the first moment that CP points to somewhere outside the sequence.If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one.
If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence.
If at any moment the CP goes outside of the sequence the execution is terminated.It's obvious the every program in this language terminates after some steps.We have a sequence s1, s2, ..., sn of "<", ">" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, ..., sr as an independent program in this language.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100) — represents the length of the sequence s and the number of queries.The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces.The next q lines each contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the i-th query.Output
For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in inputSample Input
7 41>3>22<1 34 77 71 7Sample Output
0 1 0 1 0 0 0 0 0 02 2 2 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 02 3 2 1 0 0 0 0 0 0HINT
Source
有一个cp初始指向第一个字符,dp指明cp移动的方向,初始为右,cp在数字字符时,打印出当前字符,然后根据dp移动cp,并且将其减一,若已为0则删除,若当前指向的是方向字符,则改变dp指明的方向,然后根据dp移动cp,若有连续的两个指向字符在一起,删除前面一个。#include <iostream>#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define pi acos(-1)
char str[105];
char ori[105];
int x[10];
bool isdigit(char ch) {
if (ch >= '0' && ch <= '9') {
return true;
}
return false;
}
int main() {
int n, q;
while (~scanf("%d%d", &n, &q)) {
scanf("%s", str + 1);
int a, b;
while (q--) {
for (int i = 1; i <= n; ++i) {
ori[i] = str[i];
}
scanf("%d%d", &a, &b);
int cp = a, c = 0, pre;
bool dp = true;
bool er[105];
memset(er, false, sizeof(er));
memset(x, 0, sizeof(x));
while (cp >= a && cp <= b) {
if (isdigit(str[cp])) {
c = 0;
int t = str[cp] - '0';
x[t]++;
if (t == 0) {
er[cp] = true;
}
else {
str[cp] -= 1;
}
}
else {
c++;
if (c == 2) {
er[pre] = true;
c = 1;
}
pre = cp;
if (str[cp] == '>') {
dp = true;
}
else {
dp = false;
}
}
if (dp) {
cp++;
}
else {
cp--;
}
while (er[cp] && cp >= a && cp <= b) {
if (dp) {
cp++;
}
else {
cp--;
}
}
}
printf("%d", x[0]);
for (int i = 1; i <= 9; ++i) {
printf(" %d", x[i]);
}
putchar('\n');
for (int i = 1; i <= n; ++i) {
str[i] = ori[i];
}
}
}
return 0;
}
相关文章推荐
- poj1038(状态压缩)
- POJ 1038 Bugs Integrated, Inc.3进制压缩
- Bugs IntegratedInc poj 1038
- 题目1038:Sum of Factorials 九度
- lightOJ 1038 Race to 1 Again
- HDU解题报告——1038
- poj 1038 Bugs Integrated, Inc.(动态规划状态压缩)
- Ural1094 && Ural1038(简单字符串)
- hihocoder1038 01背包
- #1038 : 01背包 ( 01 背包,基础DP)
- [LOJ 1038] Race to 1 Again
- 南邮 OJ 1038 最小代价树
- PAT(甲级)1038
- 【ZJOI2008】【BZOJ1038】瞭望塔
- bzoj1038[ZJOI2008]瞭望塔
- ZOJ 1038 T9
- PAT basic 1038
- lightoj 1038 - Race to 1 Again 【概率dp】
- Lightoj1038 概率dp + 因数分解