Codeforces_358B_Dima and Text Messages(字符串)

来源:互联网 发布:windows编程7 编辑:程序博客网 时间:2024/04/29 20:11

题型:简单题


题意:

       将n个单词连成一句话后进行编码,每个单词之间加个“<3”(包括首尾),之后在这段字符串中的任意位置加上小写字母、数字、大于号>、小于号<  都视为合法。现在给出一个字符串b,问它是否合法。


分析:

        先将一系列单词构成"<3word1<3word2<3word3<3"的形式,设为a字符串,长度为a_len ,然后拿a与b进行比较,如果碰到非法字符就直接跳出;设一个计数变量cnt,如果对应位置上字符不相等的,暂且算它是随意插入的合法字符, 然后在b上的扫描指针向后退以为,继续比较;如果相应位置相等了,cnt++,a和b上的扫描指针都向后退。最后看cnt与a_len是否相等。


代码:

#include<iostream>#include<cstring>#include<cmath>#include<cstdio>using namespace std;char a[1234567],b[1234567],c[123465];int main(){    int n;    while(~scanf("%d",&n)){        int cnt=0;        b[cnt++]='<';        b[cnt++]='3';        while(n--){            scanf("%s",c);            int c_len=strlen(c);            for(int i=0;i<c_len;i++){                b[cnt++]=c[i];            }            b[cnt++]='<';            b[cnt++]='3';        }        scanf("%s",a);        int a_len=strlen(a);        int count=0;        bool flag=true;        for(int i=0,j=0;i<a_len;i++){            if(a[i]==b[j]){                j++;                count++;            }            else{                if((a[count]>='a'&&a[count]<='z')||(a[count]>='0'&&a[count]<='9')||(a[count]=='<')||(a[count]=='>')){                    continue;                }                else{                    flag=false;                    break;                }            }        }        if(!flag) printf("yes\n");        else{            if(count==cnt) printf("yes\n");            else printf("no\n");        }    }    return 0;}/*3iloveyou<3i<3love<23you<37iamnotmaininthefamily<3i<>3am<3the<3<main<3in<3the<3><3family<3*/


原创粉丝点击