UVa10340

All in All

题意:字符串匹配

#include <stdio.h>
#include <string.h>

char S[200000];
char P[200000];
int next[200000];

int KMP(int pos, int len1, int len2)
{
    int i = pos, j = 1, k = 0;
    next[1] = 0;
    while (j < len1)
    if (k == 0 || P[k - 1] == P[j - 1]) j++, k++, next[j] = k;
    else k = next[k];

    j = 1;
    while (i <= len2 && j <= len1)
    {
        if (j == 0 || S[i - 1] == P[j - 1]) i++, j++;
        else j = next[j];
    }
    return j > len1 ? i - len1 : 0;
}

int main(int argc, char *argv[])
{
    int len1, len2, i, t1, t2, j, b, c;

    while(scanf("%s%s", P, S) != EOF)
    {
        t1 = t2 = 1;
        b = -1;
        len1 = strlen(S);
        len2 = strlen(P);

        if(KMP(0, len2, len1) == 0)
            t2 = 0;
        if(t2 == 0)
        {
            for(i = 0; i < len2; i++)
            {
                c = 0;
                for(j = 0; j < len1; j++)
                {
                    if(P[i] == S[j] && b < j)
                    {
                        b = j;
                        c = 1;
                        break;
                    }
                }
                if(c != 1)
                    t1 = 0;
            }
        }

        printf("%s\n", t1 || t2 ? "Yes" : "No");
        memset(S, 0, sizeof(S));
        memset(P, 0, sizeof(P));
        memset(next, 0, sizeof(next));
    }
    return 0;
}

UVa10340,布布扣,bubuko.com

时间: 2025-01-11 19:25:27

UVa10340的相关文章

UVa10340.All in All

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1281 13845990 10340 All in All Accepted C++ 0.026 2014-07-07 15:05:55 All in All Input: standard input Output: standard output Time Limit: 2 se

UVA10340 POJ1936 ZOJ1970 All in All

问题链接:UVA10340 POJ1936 ZOJ1970 All in All.入门练习题,用C语言编写程序. 题意简述:输入两个字符串s和t,看s是否是t的子串.t中的字符可以任意删除,只要顺序匹配字符串就可以. AC的C语言程序如下: /* UVA10340 POJ1936 ZOJ1970 All in All */ #include <stdio.h> #include <string.h> #define MAXN 110000 char s[MAXN], t[MAXN]

UVA10340 All in All子序列

Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limit: 32 MB You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a cleve

UVA10763:Foreign Exchange&amp;&amp;UVA10340: All in All(水题)

10763:水题不解释直接贴代码. #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <math.h> #include <queue> #define eps 1e-9 typedef long long ll; using namespace std; int n; int d[500100]; int

UVa10340 All in All (子序列)

输入两个字符串s和t,判断是否可以从t中删除0个或者多个字符(其他字符顺序不变),得到字符串s.例如,abcde可以得到bce,但无法得到cb. Input 输入多组数据 每组一行包含两个字符串s和t,两字符串之间用空格隔开. 字符串长度在100000以内 Output 输出Yes或No Sample Input sequence subsequence person compression VERDI vivaVittorioEmanueleReDiItalia caseDoesMatter

算法竞赛入门经典第二版第三章习题

写这个的原因是看到一位大神的习题答案总结,于是自己心血来潮也想写一个这个,目的主要是督促自己刷题吧,毕竟自己太弱了. 习题3-1 得分 UVa 1585 大致就是设置一个变量记录到当前为止的连续的O的数量,碰到X就变0,水题. #include<stdio.h> #include<ctype.h> #include<string.h> char s[90]; int main(void) { int length,n,sum,num; scanf("%d&qu

All in All,( UVa, 10340 )

题目链接 :https://vjudge.net/problem/UVA-10340 注意数组开辟大小,太小会runtime error: 1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 10000000 4 char s[maxn],t[maxn]; 5 6 int main() 7 { 8 while (scanf("%s %s",s,t)!=EOF) 9 { 10 int j = 0; 11

2019年2月做题记录

UVA10082 (字符串常量水题) UVA272 (字符串替换水题) UVA401 (回文串镜像串水题) UVA340 (模拟题) UVA1583 (打表水题) UVA1584 (暴力) UVA1585 (模拟) UVA1586 (数学) UVA1225 (打表水题) UVA455 (KMP算法) UVA232 (模拟+思维) UVA202 (除法高精度水题) UVA1587 (思维) UVA10340 (模拟,定序求交集) 原文地址:https://www.cnblogs.com/Aya-U