您的位置:首页 > 其它

poj 1936 All in All 简单的字符串匹配

2013-12-17 13:14 204 查看
All in All

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 26339Accepted: 10691
Description
You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings
are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.

Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.

Input
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace.The length of s and t will no more than 100000.
Output
For each test case output "Yes", if s is a subsequence of t,otherwise output "No".
Sample Input
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

Sample Output
Yes
No
Yes
No

Source Code
Problem: 1936		
Memory: 560K		Time: 0MS
Language: GCC		Result: Accepted

    Source Code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 100005

    int SolveCase_1(char *s, char *t)
    {
        char *temp_s = s;
        int count_s=0 ,number, i, j;
        while(*temp_s != '\0')
        {
            count_s++;
            temp_s ++;
        }
        for(i=0, j=0, number=0; i < MAX; i++)
        {
            if(t[j]=='\0' || s[i] =='\0') break;
            if(t[j]==s[i]){
                number++;
                j++;
            }else{
                number=0;
                j++;
            }
        }
        if(number == count_s) return 1;
        return 0;
    }
    int SolveCase_2(char *s, char *t)
    {
        int count_t = 0;
        char *temp_t = t;
        char *temp_s = s;
        int count_s =0, i, j;
        while(*temp_t != '\0')
        {
            temp_t ++;
            count_t ++;
        }
        while(*temp_s != '\0')
        {
            temp_s ++;
            count_s ++;
        }
        for(i = 0,j = 0; i < count_t; i++)
        {
            if(s[j] == '\0' || t[i]=='\0') break;
            if(s[j]==t[i]) j++;
            if(j==count_s) break;
        }
        if(j==count_s) return 1;
        return 0;
    }
    int main()
    {
     //   freopen("i.txt","r",stdin);
        char s[MAX], t[MAX];
        memset(s, '\0', sizeof(s));
        memset(t, '\0', sizeof(t));
        while(scanf("%s%s", s, t) !=EOF)
        {
            if(SolveCase_1(s, t)!=1){
                if(SolveCase_2(s, t)!=1)
                    printf("No\n");
                else printf("Yes\n");
            }else printf("Yes\n");
            memset(s, '\0', sizeof(s));
            memset(t, '\0', sizeof(t));
        }
        return 0;
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: