首页 > 其他 > 详细

FZU Problem 2183 简单题(字符串处理)

时间:2015-03-23 09:41:16      阅读:256      评论:0      收藏:0      [点我收藏+]

FZU Problem 2183 简单题

Problem Description

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

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

求nString是否为cString的子串,如果是输出True,否则输出False.cString的长度clen的范围是0

Sample Input

acb[3]d[5]e
bd

Sample Output

True

题目大意:中文题目。

解题思路:将后一个没有压缩的字符串进行压缩,然后与第一个压缩过的字符串进行比较,注意比较时要注意第一个和最后一个:两者的字符要相同,并且前一个的字符出现次数要大于等于后一个。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
#define N 1005
typedef long long ll;
struct C{
    int cnt;
    char ch;
};
C A[N], B[N];
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++;
            }
        }
        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;
}

FZU Problem 2183 简单题(字符串处理)

原文:http://blog.csdn.net/llx523113241/article/details/44539687

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!