Advanced Fruits

The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them.
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property.

A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.

Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.

Input

Each line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters.

Input is terminated by end of file.

Output

For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.

Sample Input

apple peach
ananas banana
pear peach

Sample Output

appleach
bananas
pearch

回溯

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[1010][1010];
char a[1010],b[1010],s[1010];
int main()
{
    int i,j,l1,l2,k;
    char c[1010];
    while(~scanf("%s%s",a+1,b+1))
    {
        memset(dp,0,sizeof(dp));
        l1=strlen(a+1);
        l2=strlen(b+1);
        for(i=1; i<=l1; i++)
            for(j=1; j<=l2; j++)
            {
                if(a[i]==b[j])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
            }
        i=l1,j=l2,k=0;
        while(dp[i][j])
        {
            if(a[i]==b[j])
            {
                s[k++]=a[i];
                i--;
                j--;
            }
            else if(dp[i-1][j]>dp[i][j-1])
            {
                s[k++]=a[i];
                i--;
            }
            else if(dp[i-1][j]<=dp[i][j-1])
            {
                s[k++]=b[j];
                j--;
            }
        }
        while(i>=1)
        {
            s[k++]=a[i];
            i--;
        }
        while(j>=1)
        {
            s[k++]=b[j];
            j--;
        }
        for(i=k-1;i>=0;i--)
            printf("%c",s[i]);
        printf("\n");
    }
    return 0;
}

 

递归

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
 
char s1[1000],s2[1000];
int len1,len2,dp[1000][1000],mark[1000][1000];
 
void LCS()
{
    int i,j;
    memset(dp,0,sizeof(dp));
    for(i = 0;i<=len1;i++)
    mark[i][0] = 1;
    for(i = 0;i<=len2;i++)
    mark[0][i] = -1;
    for(i = 1; i<=len1; i++)
    {
        for(j = 1; j<=len2; j++)
        {
            if(s1[i-1]==s2[j-1])
            {
                dp[i][j] = dp[i-1][j-1]+1;
                mark[i][j] = 0;
            }
            else if(dp[i-1][j]>=dp[i][j-1])
            {
                dp[i][j] = dp[i-1][j];
                mark[i][j] = 1;
            }
            else
            {
                dp[i][j] = dp[i][j-1];
                mark[i][j] = -1;
            }
        }
    }
}
 
void PrintLCS(int i,int j)
{
    if(!i && !j)
        return ;
    if(mark[i][j]==0)
    {
        PrintLCS(i-1,j-1);
        printf("%c",s1[i-1]);
    }
    else if(mark[i][j]==1)//根据回溯的位置进行输出
    {
        PrintLCS(i-1,j);
        printf("%c",s1[i-1]);
    }
    else
    {
        PrintLCS(i,j-1);
        printf("%c",s2[j-1]);
    }
}
 
int main()
{
    while(~scanf("%s%s",s1,s2))
    {
        len1 = strlen(s1);
        len2 = strlen(s2);
        LCS();
        PrintLCS(len1,len2);
        printf("\n");
    }
 
    return 0;
}

<think>好的,用户现在问的是SQLite中的模糊匹配。首先,我需要确认他们对模糊匹配的理解程度,可能他们是想在数据库里进行模式匹配,比如查找包含特定字符串的记录。SQLite常用的模糊匹配方法通常是使用LIKE操作符或者GLOB,可能还有正则表达式,不过正则表达式需要用户定义的函数支持。 用户之前的问题是关于删除字段中空格前的数据,现在转向模糊匹配,可能是他们在处理数据清洗或查询时遇到了相关需求。需要考虑到他们可能需要具体的例子和语法说明。 接下来,我需要回忆SQLite中LIKE和GLOB的区别。LIKE使用%和_作为通配符,而GLOB使用*和?,并且区分大小写。此外,需要提到REGEXP的使用限制,因为SQLite默认没有内置的正则表达式支持,但可以通过扩展实现。 用户可能想知道基本用法,比如如何匹配开头、中间或结尾的字符串,以及如何处理大小写敏感的情况。同时,可能需要提醒他们注意性能问题,比如前导通配符可能导致全表扫描,影响查询速度。 还要考虑用户是否需要实际案例,比如查询名字中包含特定字符的记录,或者更复杂的模式。此外,转义特殊字符的方法也是一个重要点,避免因为特殊字符导致查询错误。 是否需要提到使用索引优化模糊查询?虽然对于前导通配符的情况索引可能无效,但如果是固定前缀,比如LIKE 'abc%',索引还是可以派上用场的。这可能对用户优化查询性能有帮助。 另外,用户可能在处理字符串时遇到需要转义的情况,比如查询包含%或_本身的记录,这时候需要使用ESCAPE子句,比如LIKE '%\%%' ESCAPE '\',这样可以正确匹配百分号。 最后,要确保回答结构清晰,分点说明,每个部分都有示例,让用户能够逐步理解和应用。同时,注意事项部分需要涵盖常见陷阱,比如大小写敏感性和性能问题,以及使用正则表达式的限制。</think>在 SQLite 中实现模糊匹配主要通过 `LIKE` 和 `GLOB` 操作符,还支持正则表达式(需依赖扩展)。以下是具体用法和场景说明: --- ### **1. LIKE 操作符(基本模糊匹配)** - **语法**:`WHERE column LIKE pattern` - **通配符**: - `%`:匹配任意长度(包括零长度)的字符串。 - `_`:匹配单个字符。 - **示例**: ```sql -- 匹配以 "apple" 开头的字符串 SELECT * FROM fruits WHERE name LIKE 'apple%'; -- 匹配包含 "berry" 的字符串 SELECT * FROM fruits WHERE name LIKE '%berry%'; -- 匹配第三个字符为 "a" 的字符串(如 "banana") SELECT * FROM fruits WHERE name LIKE '__a%'; ``` --- ### **2. GLOB 操作符(区分大小写的模式匹配)** - **语法**:`WHERE column GLOB pattern` - **通配符**: - `*`:匹配任意长度字符串(类似 `%`)。 - `?`:匹配单个字符(类似 `_`)。 - **特点**:区分大小写,且使用 UNIX 风格的通配符。 - **示例**: ```sql -- 匹配以 "Apple" 开头的字符串(区分大小写) SELECT * FROM fruits WHERE name GLOB 'Apple*'; ``` --- ### **3. 正则表达式(需加载扩展)** SQLite **默认不支持正则表达式**,但可通过用户自定义函数或加载扩展(如 `regexp`)实现: - **语法**:`WHERE column REGEXP pattern` - **示例**: ```sql -- 匹配包含数字的字符串(需提前加载正则扩展) SELECT * FROM products WHERE description REGEXP '\d+'; ``` > **注意**:需通过 `SQLITE_ENABLE_REGEXP` 编译选项或外部库启用正则功能。 --- ### **4. 转义特殊字符** 若需匹配通配符本身(如 `%`、`_`),需使用 `ESCAPE` 子句: ```sql -- 查找包含 "10%" 的字符串(如 "折扣10%") SELECT * FROM sales WHERE note LIKE '%10\%%' ESCAPE '\'; ``` --- ### **5. 常见使用场景** | **场景** | **示例** | **说明** | |-------------------------|-----------------------------------|-----------------------------| | 匹配开头/结尾 | `LIKE 'A%'` 或 `LIKE '%z'` | 筛选以特定字符开始/结束的数据 | | 包含特定片段 | `LIKE '%error%'` | 查找日志中的错误信息 | | 固定格式匹配 | `LIKE '__-___-____'` | 匹配电话号格式如 "12-345-6789" | | 排除特定内容 | `NOT LIKE '%test%'` | 过滤测试数据 | --- ### **注意事项** 1. **性能影响**:模糊匹配(尤其是以 `%` 开头)可能导致全表扫描,建议: - 对高频查询字段建立索引(但对前导通配符无效)。 - 结合其他条件缩小查询范围。 2. **大小写敏感**: - `LIKE` 默认不区分大小写(取决于 SQLite 编译配置)。 - 若需强制区分大小写,用 `GLOB` 或 `COLLATE BINARY`: ```sql SELECT * FROM table WHERE column LIKE 'A%' COLLATE BINARY; ``` 3. **扩展依赖**:正则表达式需额外配置,确保环境支持。 --- ### **实战示例** ```sql -- 创建测试表 CREATE TABLE books ( id INTEGER PRIMARY KEY, title TEXT ); -- 插入数据 INSERT INTO books (title) VALUES ('SQLite Guide'), ('Python for Beginners'), ('Advanced SQL Techniques'); -- 模糊查询:标题包含 "SQL" 或 "sql"(不区分大小写) SELECT * FROM books WHERE title LIKE '%sql%'; -- 结果: -- 1 | SQLite Guide -- 3 | Advanced SQL Techniques ``` --- 通过合理使用 `LIKE`、`GLOB` 和正则表达式,可高效实现 SQLite 中的模糊匹配需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值