hdu6144(模拟)

Arithmetic of Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 132    Accepted Submission(s): 96


Problem Description
众所周知,度度熊非常喜欢数字。

它最近在学习小学算术,第一次发现这个世界上居然存在两位数,三位数……甚至N位数!

但是这回的算术题可并不简单,由于含有表示bomb的#号,度度熊称之为 Arithmetic of Bomb。



Bomb Number中的bomb,也就是#号,会展开一些数字,这会导致最终展开的数字超出了度度熊所能理解的范畴。比如”(1)#(3)”表示”1”出现了3次,将会被展开为”111”,

同理,”(12)#(2)4(2)#(3)”将会被展开为”12124222”。

为了方便理解,下面给出了Bomb Number的BNF表示。

```
<bomb number> := <bomb term> | <bomb number> <bomb term>
<bomb term> := <number> | '(' <number> ')' '#' '(' <non-zero-digit> ')'
<number> := <digit> | <digit> <number>
<digit> := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
<non-zero-digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
```

请将Bomb Number中所有的#号展开,由于数字可能很长,结果对 1 000 000 007 取模。
 

Input
第一行为T,表示输入数据组数。

每组数据包含一个Bomb Expression。


- 1≤T≤100

- 1≤length(Bomb Number)≤1000
 

Output
对每组数据输出表达式的结果,结果对 1 000 000 007 取模。
 

Sample Input
  
  
4 1 (1)#(3) (12)#(2)4(2)#(3) (12)#(5)
 

Sample Output
  
  
1 111 12124222 212121205
 

Source
#include<cstdio>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;
char str[1010];
char a[1010];
char b[1010];
int main()
{
	int t;
	queue<char>q;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",str);//读入 
		//cout<<"!"<<endl;
		int len=strlen(str);
		int cnt,l,r,ln,rn;
		int f=0;
		ln=0;rn=0;
		cnt=0;
		int num=0;
		int c=0;
		for(int i=0;i<len;i++)
		{
			if(str[i]=='('&&f==0)//在#号前的括号 
			{
				ln=1; 
			}
			else if(str[i]=='('&&f==1)//在#号后的括号 
			{
				rn=1;
			}
			else if(str[i]=='#')//有#号说明前面已经有一个括号了 
			{
				f=1;
			}
			else if(str[i]==')'&&f==0)//在#号前的第一个反括号 
			{
				f=0;
			}
			else if(str[i]==')'&&f==1)//在#号后的第一个反括号 
			{
				f=0;
				ln=0,rn=0;
				num=0;
				for(int i=0;i<c;i++)//计算多少个 
				{
					num=num*10+b[i]-'0';
				}
				for(int i=0;i<num;i++)//把这几个放入队列 
				{
					for(int j=0;j<cnt;j++)
					{
						q.push(a[j]);
					}
				}
				num=0;cnt=0;c=0;
			}
			else{//字符串是数字时 
				if(ln==1&&f==0)
					a[cnt++]=str[i]; 
				if(ln==0&&f==0)
					q.push(str[i]);
				if(rn==1&&f==1)
					b[c++]=str[i];	
			}
		}
		//大数取模模板  
        ll MODZ=1000000007;  
        len = q.size();  
        ll ansnum = 0;  
        int k;
        for(int i = 0; i < len; ++i)
		{  
        	k=q.front();
            ansnum = ansnum*10 + (k-'0');  
            ansnum %= MODZ; 
			q.pop(); 
        }  
        cout<<ansnum<<endl;  
	}	
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值