当前位置: 首页 > backend >正文

字符串接龙

题目
讲解

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <queue>
using namespace std;
int main() {string beginStr, endStr, str;vector<string> allstr;int n;cin >> n;unordered_map<string,int> strMap;cin >> beginStr >> endStr;allstr.push_back(beginStr);strMap[beginStr]=0;for (int i = 0; i < n; i++) {cin >> str;strMap[str]=i+1;allstr.push_back(str);}allstr.push_back(endStr);strMap[endStr]=n+1;vector<vector<int>> grid(n+2,vector<int>(n+2,0));for(int k=0;k<n+2;k++){string curstr=allstr[k];// cout<<curstr<<"   ";for (int i = 0; i < curstr.size(); i++) {string newWord = curstr; // 用一个新字符串替换str,因为每次要置换一个字符// 遍历26的字母for (int j = 0 ; j < 26; j++) {newWord[i] = j + 'a';// 字符串集合里出现了newWord,并且newWord没有被访问过if (strMap.find(newWord) != strMap.end()){int index=strMap.find(newWord)->second;// cout<<index<<" "<<newWord<<" ";grid[k][index]=1;grid[index][k]=1;}}}grid[k][k]=2;// cout<<endl;}// for(int i=0;i<n+2;i++){//     for(int j=0;j<n+2;j++){//         cout<<grid[i][j]<<" ";//     }//     cout<<endl;// }vector<int> visited(n+1,0);queue<pair<int,int>> que;que.push({0,1});while(!que.empty()){auto cur=que.front();int curindex=cur.first;int curlength=cur.second;que.pop();visited[curindex]=1;for(int i=0;i<n+2;i++){if(grid[curindex][i]==1){if(i==n+1){cout<<curlength+1;return 0;}grid[curindex][i]=2;que.push({i,curlength+1});}}}
}
http://www.xdnf.cn/news/12043.html

相关文章:

  • Java 大视界 — Java 大数据在智能安防视频监控中的异常事件快速响应与处理机制
  • 未来的AI 终端
  • 系统性学习C语言-第十四讲-深入理解指针(4)
  • 《仿盒马》app开发技术分享-- 商品搜索页(顶部搜索bar热门搜索)(端云一体)
  • 中级统计师-经济学基础知识-第一章 经济学基础
  • android与Qt类比
  • 重要通知:6月申报纳税期限延至6月16日(附2025年办税日历 图文版)
  • 树莓派超全系列教程文档--(53)如何使用 SCP 共享文件
  • 嵌入式开发之STM32学习笔记day18
  • 手写Promise实例方法finally
  • 肺癌靶向治疗耐药困境下的新探索
  • LLM应用开发(九)- 幻觉及如何缓解
  • Authentication failed(切换了新的远程仓库tld)
  • 区块链技术发展现状与应用前景分析
  • Rocket客户端消息确认机制
  • JAVA:String类详解
  • 华为云Flexus+DeepSeek征文|Flexus云服务器单机部署+CCE容器高可用部署快速搭建生产级的生成式AI应用
  • (LeetCode 每日一题)3403. 从盒子中找出字典序最大的字符串 I (贪心+枚举)
  • 12.3Swing控件1
  • Java并发编程实战 Day 7:并发集合类详解
  • Docker轻松搭建Neo4j+APOC环境
  • 《PyTorch Hub:解锁深度学习模型的百宝箱》
  • 物流数据接口新玩法:跨境订单处理效率提升200%
  • echarts显示/隐藏标签的同时,始终显示饼图中间文字
  • 简数采集技巧之快速获取特殊链接网址URL方法
  • Playwright 测试框架 - Python
  • 软件工程专业的本科生应该具备哪些技能
  • 【Bluedroid】蓝牙启动之gatt_init 流程源码解析
  • DrissionPage爬虫包实战分享
  • 汽车加气站操作工证考试重点