![](http://acm.fzu.edu.cn/image/problem.gif)
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;
}