题目大意:
让你还原一个字符串,有括号就看他前面的,比如2(3)就是33,然后展开看他长度,然后输出。
思路:
直接递归,没看到括号就ans++,看到括号就拆,但我因打错文件名RE了一晚上
C o d e Code Code:
#include<cstdio>
#include<iostream>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
using namespace std;
string ch;
long long dfs(long long head,long long now)//head为起始点,now为当前点
{
int len=0,last=0,check;//len为head~now的长度,last是上一个,check是用于判断括号找完没。
while(head<=now)//能找
{
if(ch[head]=='(')//左括号
{
check=1;
for(int i=head+1;i<=now;++i)
{
if(ch[i]=='(') check++;
if(ch[i]==')') check--;
if(!check)//找完了
{
len+=last*dfs(head+1,i-1);//递归求值
last=0;head=i;
break;
}
}
}
else if(ch[head+1]=='(') last=ch[head]-'0';//否则记录下来
else len++;//长度++
head++;//往前走
}
return len;
}
int main()
{
fre(b);//文件输入输出
cin>>ch;
int n=ch.size();
printf("%d",dfs(0,n-1));//输出
}