P1019 [NOIP2000 提高组] 单词接龙 刷题笔记

news/2024/7/29 19:32:52 标签: 算法

P1019 [NOIP2000 提高组] 单词接龙 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路来自 大佬 Chardo 的个人中心 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

匹配 :

将 第一个字符串末尾 和第二个字符串第一个开始匹配 

如果 j<i这段走完了 

flag还没被修改 说明 已经存在重叠部分 

可以连接 

反之 如果匹配不成功 

将 第一个字符串的指向往左移动一位 再和第二个串 开头字符看是否匹配

如果i=3,j=3说明有一段长度为3的串匹配成功了 可以返回长度3了

#include<iostream>
#include<string.h>
using namespace std;
string str[20];
int use[20];
int n,ans;
int  clink(string x,string y){
    
    for(int i=1;i<min(x.length() ,y.length());i++ ){
        int flag=1;
        for(int j=0;j<i;j++){
            if(x[x.length() -i+j]!=y[j]){
                flag=0;
            }
        }
        if(flag){
            return i;
        }
        
    }
    return 0;
    
}
void slove(string nowstr ,int nowlen){
    ans=max(ans,nowlen);
    for(int i=0;i<n;i++){
        if(use[i]>=2){
            continue;
        }
        int c=clink(nowstr,str[i]);
        if(c>0){
            use[i]++;
            slove(str[i],nowlen+str[i].length() -c);
            use[i]--;
        }
    }
    
}
int main(){
    
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>str[i];
    }
    cin>>str[n];
    slove(' '+str[n],1);
    cout<<ans;
    
    return 0;


http://www.niftyadmin.cn/n/5297214.html

相关文章

【零基础入门VUE】在 Vue 中构建复杂表单

✍面向读者&#xff1a;所有人 ✍所属专栏&#xff1a;零基础入门VUE专栏https://blog.csdn.net/arthas777/category_12537076.html 目录 v-modelVue 中的 指令 Vue 中的组件 没有构建步骤 随着构建步骤 注册 VUE 组件 Vue 道具 VUE 中的道具声明 在 VUE 中传递 PROP…

Java Iterable和Iterator接口区别是什么?

Java Iterable和Iterator接口区别是什么&#xff1f; Iterable 和 Iterator 是 Java 集合框架中的两个关键接口&#xff0c;用于支持遍历集合元素的操作。它们之间的区别如下&#xff1a; Iterable 接口&#xff1a;这是一个顶层接口&#xff0c;定义了一个返回迭代器的方法 i…

go-pg框架如何查询指定的列

目录 指定单列 指定多列 指定单列 通过id查询name列 var name string .Where("id ?", id).Column("name").Select(&name) 指定多列 通过class查询age、name type AgeAndName struct {Age intName string }var records []AgeAndName .Where(…

Linux:apache优化(4)—— 隐藏版本号

运行环境 yum -y install apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre gcc make zlib-devel 源码包配置 ./configure --prefix/usr/local/httpd --enable-cgi --enable-rewrite --enable-so --enabl…

JSoup 爬虫遇到的 404 错误解决方案

在网络爬虫开发中&#xff0c;使用JSoup进行数据抓取是一种常见的方式。然而&#xff0c;当我们尝试使用JSoup来爬虫抓取腾讯新闻网站时&#xff0c;可能会遇到404错误。这种情况可能是由于网站的反面爬虫机制检测到了我们的爬虫行为&#xff0c;从而拒绝了我们的请求。 假设我…

2. SQL - 约束

1 主键约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录。 主键必须包含唯一的值。 主键列不能包含 NULL 值。 每个表都应该有一个主键&#xff0c;并且每个表只能有一个主键。 添加主键约束 创建表时&#xff0c;在字段描述处&#xff0c;声明指定字段为主键创建表时&…

Java excel单元格内容读取为字符串格式

导出数据到Excel&#xff0c;并把单元格内容转为字符串。 // 将单元格内容转化为字符串 private static String convertCellValueToString(Cell cell) {if (null cell) {return null;}String returnValue null;switch (cell.getCellType()) {case STRING: //字符串returnVa…

命令行fdisk扩展ubuntu 扩展分区sda2下的sda5 挂载根路径下的主分区

具体如下 rootubuntu:~# fdisk /dev/sdaWelcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.#查询现有分区情况&#xff0c;40G需要扩展到50G Command (m for help): p Di…