Codeforces Round #806 (Div. 4) C. Cypher

Luca has a cypher made up of a sequence of nn wheels, each with a digit aiai written on it. On the ii-th wheel, he made bibi moves. Each move is one of two types:

  • up move (denoted by UU): it increases the ii-th digit by 1. After applying the up move on 9, it becomes 0.
  • down move (denoted by DD): it decreases the ii-th digit by 1. After applying the down move on 0, it becomes 9.

 

                                       Example for n=4. The current sequence is 0 0 0 0.

Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1001≤n≤100) — the number of wheels.

The second line contains nn integers aiai (0≤ai≤90≤ai≤9) — the digit shown on the ii-th wheel after all moves have been performed.

Then nn lines follow, the ii-th of which contains the integer bibi (1≤bi≤101≤bi≤10) and bibi characters that are either UU or DD — the number of moves performed on the ii-th wheel, and the moves performed. UU and DD represent an up move and a down move respectively.

Output

For each test case, output nn space-separated digits  — the initial sequence of the cypher.

Example

input

3 
3 
9 3 1 
3 DDD 
4 UDUU 
2 DU 
2 
0 9 
9 DDDDDDDDD 
9 UUUUUUUUU 
5 
0 5 9 8 3 
10 UUUUUUUUUU 
3 UUD 
8 UUDUUDDD 
10 UUDUUDUDDU 
4 UUUU

output

2 1 1 
9 0 
0 4 9 6 9 

Note

In the first test case, we can prove that initial sequence was [2,1,1][2,1,1]. In that case, the following moves were performed:

  • On the first wheel: 2→D1→D0→D92→D1→D0→D9.
  • On the second wheel: 1→U2→D1→U2→U31→U2→D1→U2→U3.
  • On the third wheel: 1→D0→U11→D0→U1.

The final sequence was [9,3,1][9,3,1], which matches the input.

Idea

本题是以密码锁为背景设定的,给定一个数列及从原数列得到该数列的过程,要我们求解原数列。

由于是逆推,所以遇到D,数就加一,反之遇到U,数要减一。特别地,遇到9时加一为0,遇到0时减一为9。

Solution

#include<stdio.h>
int Operate(int a,int h,char s[15])
{
    int k=a,i;
    for(i=0; i<h; i++)
    {
        if(s[i]=='D')
        {
            if(k==9)
                k=0;
            else
                k++;
        }
        if(s[i]=='U')
        {
            if(k==0)
                k=9;
            else
                k--;
        }
    }
    return k;
}
int main()
{
    int n,t,h,i,j,a[110];
    char s[15];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(i=0; i<n; i++)
        {
            scanf("%d %s",&h,s);
            a[i]=Operate(a[i],h,s);
        }
        for(i=0; i<n; i++)
            printf("%d ",a[i]);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值