(水题)简单题

 Problem 2183 简单题

Accept: 94    Submit: 399
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

现在有一些被简单压缩的字符串,例如:a[120]代表120个a。对于字符串acb[3]d[5]e相对于acbbbddddde

现在给你两个字符串cString, nString.一个是被压缩过的字符串,另一个没有被压缩。

求nString是否为cString的子串,如果是输出True,否则输出False.cString的长度clen的范围是0<clen<1000, nString的长度的nlen的范围是0<nlen<1000;cString只包含小写26个字母,[],数字(大于0小于10^9)。nString只包含小写26个字母。

 Sample Input

acb[3]d[5]ebd

 Sample Output

True

 Source

FOJ有奖月赛-2015年03月


水题,注意 cString 可能有abc[3]c[1]ccde 的情况。注意预处理的技巧。

//http://acm.fzu.edu.cn/problem.php?pid=2183
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 1005;
const int INF = 0xfffffff;
const double EPS = 1e-8;
char cstr[MAXN], nstr[MAXN];
char n[MAXN * 8], c[MAXN * 8];
int clen, nlen;
LL N[MAXN], C[MAXN];
void Do0()
{
	memset(C, 0, sizeof(C));
	int len = strlen(cstr);
	int top = 0;
	char last = 0;
	for (int i = 0; i < len; i++)
	{
		if ('a' <= cstr[i] && cstr[i] <= 'z')
		{
			if (cstr[i] == last)
			{
				if (cstr[i + 1] == '[')
				{
					int tmp = 0;
					i+=2;
					while (cstr[i] != ']')
						tmp = tmp * 10 + (cstr[i++] - '0');
					C[top - 1] += tmp;
				}
				else
				{
					C[top - 1]++;
				}
			}
			else
			{
				last = cstr[i];
				c[top] = cstr[i];
				if (cstr[i + 1] == '[')
				{
					int tmp = 0;
					i+=2;
					while (cstr[i] != ']')
						tmp = tmp * 10 + (cstr[i++] - '0');
					C[top++] += tmp;
				}
				else
					C[top++] = 1;
			}
		}
	}
	clen = top;
}
void Do1()
{
	memset(N, 0, sizeof(N));
	int len = strlen(nstr);
	char last = 0;
	int top = 0;
	for (int i = 0; i < len; i++)
	{
		if (last != nstr[i])
		{
			N[top] = 1;
			n[top++] = nstr[i];
			last = nstr[i];
		}
		else
		{
			N[top-1]++;
		}
	}
	nlen = top;
}
bool Judge(int i)
{
	for (int j = 0; j < nlen; j++,i++)
	{
		if (c[i] == n[j])
		{
			if (C[i] == N[j])	continue;
			else if (C[i]>N[j])
			{
				if (j == 0 || j == nlen - 1)
					continue;
				else return false;
			}
			else return false;
		}
		else return false;
	}
	return true;
}
bool Find()
{
	for (int i = 0; i < clen; i++)
	{
		if (c[i] == n[0])
		if (Judge(i))
			return true;
	}
	return false;
}
int main()
{
	//freopen("F:\\input.txt","r",stdin);
	while (scanf("%s", cstr) != EOF){
		scanf("%s", nstr);
		Do0();
		Do1();

		printf("%s\n", Find() ? "True" : "False");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值