您的位置:首页 > 其它

FZU 2183 简单题(字符串匹配|字符串压缩)(简单)

2015-08-04 19:11 369 查看
Problem 2183 简单题

Accept: 138 Submit: 641

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

思路:

直接暴力就可以,但是要注意的是每个位置记录字母的类型和数量然后直接暴力就可以了。但是要注意c[3]c[5]这种数据。

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include<iostream>
using namespace std;
#define N 1005
typedef long long ll;
struct C{
int cnt;
char ch;
};
C A
, B
;
int main()
{
while (1)
{
for (int i = 0; i < N; i++)
{
A[i].cnt = 1;
}
char temp, t = ' ';
int cnt1 = -1, cnt2 = -1;
while (scanf("%c", &temp), temp != '\n')
{
if (t == temp) A[cnt1].cnt++;
else if (temp != '[' && t != temp)
{
cnt1++;
}

if (temp >= 'a' && temp <= 'z')
{
A[cnt1].ch = temp;
t = temp;
}
if (temp == '[')
{
int num;
scanf("%d%*c", &num);
A[cnt1].cnt = num;
}
}
t = ' ';
while (scanf("%c", &temp), temp != '\n')
{
if (temp >= 'a' && temp <= 'z' && temp != t)
{
B[++cnt2].ch = temp;
B[cnt2].cnt = 1;
t = temp;
}
else if (temp == t)
{
B[cnt2].cnt++;
}
}
/*
for(int i=0;i<=cnt1;i++)
cout<<A[i].ch<<" ";
cout<<endl;
for(int i=0;i<=cnt1;i++)
cout<<A[i].cnt<<" ";
cout<<endl;
for(int i=0;i<=cnt2;i++)
cout<<B[i].ch<<" ";
cout<<endl;
for(int i=0;i<=cnt2;i++)
cout<<B[i].cnt<<" ";
cout<<endl;
*/
int flag = 0;
for (int i = 0; i <= cnt1; i++)
{
if (A[i].ch == B[0].ch && A[i].cnt >= B[0].cnt)
{
int flag2 = 1;
for (int j = 1; j < cnt2; j++)
{
if (A[i + j].ch != B[j].ch || A[i + j].cnt != B[j].cnt)
{
flag2 = 0;
break;
}
}
if (flag2)
{
if (A[i + cnt2].ch == B[cnt2].ch && A[i + cnt2].cnt >= B[cnt2].cnt)
{
flag = 1;
break;
}
}
}
}
if (cnt1 == -1 && cnt2 == -1) break;
if (flag) printf("True\n");
else printf("False\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: