#include <iostream> #include <map> #include <vector> #include <algorithm> void brotherWord(std::vector<std::string> strVect, std::string word, int k) { std::multimap<std::string, int> rtnMap; //存放是兄弟单词的容器 std::vector<std::string>::iterator iter = strVect.begin(); while(iter != strVect.end()) { std::string curWord = *iter; if(curWord != word) //兄弟单词不能一模一样 { std::string tempword = word; std::sort(tempword.begin(), tempword.end()); std::string tempcurword = curWord; std::sort(tempcurword.begin(), tempcurword.end()); if(tempcurword == tempword) //是兄弟单词 { rtnMap.insert(std::pair<std::string, int>(curWord, 1)); } } iter++; } // print(rtnMap); std::vector<std::string> rtnVect; std::multimap<std::string, int>::iterator iterMap = rtnMap.begin(); while(iterMap != rtnMap.end()) { rtnVect.push_back(iterMap->first); iterMap++; } std::cout << rtnVect.size() << std::endl; if(rtnVect.size() > 1) std::cout << rtnVect[k-1] << std::endl; } int main() { int n; std::cin >> n; std::vector<std::string> strVect; while(n--) { std::string temp; std::cin >> temp; strVect.push_back(temp); } std::string word; std::cin >> word; int k; std::cin >> k; brotherWord(strVect, word, k); return 0; }翻译

时间: 2024-01-21 08:02:17 浏览: 30
这段代码是一个兄弟单词查找程序。 它包括了一些头文件的引入,如iostream、map、vector和algorithm。 函数brotherWord接受三个参数:一个字符串向量strVect,一个字符串word和一个整数k。 在函数中,首先定义了一个multimap容器rtnMap,用于存放兄弟单词。然后使用迭代器iter遍历strVect中的每个单词。 在每次迭代中,将当前单词curWord与word进行比较。如果它们不相同,则将它们分别进行排序,并比较排序后的结果。如果它们相等,则将curWord插入到rtnMap中。 接下来,定义了另一个字符串向量rtnVect,并使用迭代器iterMap遍历rtnMap中的每个元素。在每次迭代中,将迭代器指向的key(即兄弟单词)添加到rtnVect中。 最后,输出rtnVect的大小,并且如果rtnVect的大小大于1,则输出第k个单词。 在主函数main中,首先读取一个整数n,并初始化一个空的字符串向量strVect。然后使用循环读取n个单词,并将它们依次添加到strVect中。 接着读取一个单词word和一个整数k,并调用brotherWord函数进行兄弟单词查找。 最后,返回0表示程序正常结束。
相关问题

#include <iostream> #include <vector> #include <string> #include <algorithm> #include <map> #include <thread> #include "List.h" #include "Node.h" using namespace std; int main(void) { auto* head = new Node<int>(10); //auto *list = new List<int>(head); List<int> list(head); //list->Display(); return 0; }

这段代码包含了 List 类和 Node 类的头文件,并且定义了一个 main 函数。在 main 函数中,它首先创建了一个 Node 对象 head,数据值为 10。然后,它使用 head 对象创建了一个 List 对象 list。最后,它调用了 List 类的成员函数 Display 输出链表中的元素。 需要注意的是,由于 List 类已经包含了头节点指针 _head 和链表长度 _size,因此没有必要再使用指针变量 list 来引用 List 对象。因此,可以直接使用 List<int> list(head) 来创建对象。另外,注释掉的 list->Display() 可以直接通过 list.Display() 来调用,因为 Display 函数是一个公有成员函数。

把#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <vector> #include <string> #include <cstring> #define fast ios::sync_with_stdio(false),cin.tie(0) using namespace std; typedef pair<int, int> PII; typedef long long LL; const int N = 2e5+10; int T, t; string s[3]; void init() { /* #### #### #### # # # # # # # # #*/ // 第一行,中间行,和最后一行 // 构造成 #### #### #### for(int i = 0; i < 3; i ++ ) { if(i) s[0] += " "; s[0] += "###"; for(int j = 0; j < t; j ++ ) s[0] += "#"; } // 上半部分 // 构造成 # # # # s[1] += "# "; for(int i = 0; i < t; i ++ ) s[1] += " "; s[1] += "# #"; for(int i = 0; i < 2 * t + 5; i ++ ) s[1] += " "; s[1] += "#"; // 下半部分 // 构造成 # # # # # s[2] += "# "; for(int i = 0; i < t; i ++ ) s[2] += " "; s[2] += "# # "; for(int i = 0; i < t; i ++ ) s[2] += " "; s[2] += "#"; for(int i = 0; i < t + 3; i ++ ) s[2] += " "; s[2] += "#"; } int main() { scanf("%d", &t); int n = t * 2 + 5; init(); for(int i = 1; i <= n; i ++ ) { if(i == 1 || i == n || i == (n + 1)/2) cout << s[0] << endl; else if(i < (n + 1) / 2) cout << s[1] << endl; else cout << s[2] << endl; } return 0; } 这段代码变成C语言

把#include <iostream>改为#include <stdio.h>,把using namespace std;删除,将cout改为printf即可: ```c #include <stdio.h> #include <algorithm> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <vector> #include <string> #include <cstring> #define fast ios::sync_with_stdio(false),cin.tie(0) typedef pair<int, int> PII; typedef long long LL; const int N = 2e5+10; int T, t; char s[3][N]; void init() { // 第一行,中间行,和最后一行 // 构造成 #### #### #### for(int i = 0; i < 3; i ++ ) { if(i) strcat(s[0], " "); strcat(s[0], "###"); for(int j = 0; j < t; j ++ ) strcat(s[0], "#"); } // 上半部分 // 构造成 # # # # s[1][0] = '#'; s[1][1] = ' '; for(int i = 2; i < t+2; i ++ ) s[1][i] = '#'; s[1][t+2] = ' '; for(int i = t+3; i < 2 * t + 5; i ++ ) s[1][i] = ' '; s[1][2*t+5] = '#'; // 下半部分 // 构造成 # # # # # s[2][0] = '#'; s[2][1] = ' '; for(int i = 2; i < t+2; i ++ ) s[2][i] = '#'; s[2][t+2] = ' '; for(int i = t+3; i < 2 * t + 3; i ++ ) s[2][i] = ' '; s[2][2*t+3] = '#'; for(int i = 2*t+4; i < t+7; i ++ ) s[2][i] = ' '; s[2][t+7] = '#'; } int main() { scanf("%d", &t); int n = t * 2 + 5; init(); for(int i = 1; i <= n; i ++ ) { if(i == 1 || i == n || i == (n + 1)/2) printf("%s\n", s[0]); else if(i < (n + 1) / 2) printf("%s\n", s[1]); else printf("%s\n", s[2]); } return 0; } ```

相关推荐

优化改进以下代码#include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <iterator> using namespace std; typedef istream_iterator<string> string_input; void welcome() { cout << "******************* 变位词查找系统*********************\n" << "在词典中找出给定的字符串的所有变位词" << endl; } void readDict(vector<string> & dictionary) { cout << "首先,请输入词典的文件名称:" << endl; string dictionary_name; cin >> dictionary_name; ifstream ifs(dictionary_name.c_str()); if (!ifs.is_open()) { cerr << "异常:文件"<< dictionary_name << "没有找到 " << endl; exit(1); } cout << "词典读入中 ..." << flush; copy(string_input(ifs), string_input(), back_inserter(dictionary)); sort(dictionary.begin(),dictionary.end()); cout << "词典包含有 " << dictionary.size() << " 个单词\n\n"; ifs.close(); } void analyseAnagram(const vector<string> & dictionary) { cout << "请输入单词(或任意字母序列)" << endl; for (string_input p(cin); p != string_input(); ++p) { cout << "查找输入单词的变位词中..." << endl; string word = *p; sort(word.begin(), word.end()); bool found_one = false; do { if (binary_search(dictionary.begin(), dictionary.end(), word)) { cout << " " << word ; found_one = true; } } while (next_permutation(word.begin(), word.end())); if (!found_one) cout << " 抱歉,没有找到变位词\n"; cout << "\n请输入下一个单词 " << "(或输入Ctrl+Z终止程序 ) \n" << endl; } } int main() { welcome(); vector<string> dictionary; readDict(dictionary); analyseAnagram(dictionary); system("pause"); return 0; }

#include<algorithm> #include<iostream> #include<vector> #include<string> #include<cmath> #include <cstdio> #include <map> #include <unordered_map> using namespace std; const int INF = 0x3f3f3f3f; int n, gamma, time_count=0; int time[10]; string alpha; vector<int> Length(50005, 0); unordered_map<string, int> number; unordered_map<int, string> nega_number; vector<unordered_map<int, int>> edge(50005); vector<int> trace(50005, 0); vector<int> final_trace; void finding(string alpha) { int a=number[alpha], b; char beta; string epsilon; for(int i=9; i>=0; i--) { for(int j=1; j<10; j++) { epsilon = alpha; epsilon[i] = '0' + (int(epsilon[i]) + j) % 10; if(number.find(epsilon) != number.end() and epsilon != alpha) { b = number[epsilon]; edge[a][b]= time[i]; } } for(int j=i-1; j>=0; j--) { epsilon = alpha; beta = epsilon[j]; epsilon[j] = epsilon[i]; epsilon[i] = beta; if(number.find(epsilon) != number.end() and epsilon != alpha) { b = number[epsilon]; edge[a][b]= time[j]; } } } } void dijkstra(int i) { int beta; for(auto j : edge[i]) { beta = Length[j.first]; if(beta > Length[i] + j.second) { Length[j.first] = Length[i] + j.second; trace[j.first] = i; if(beta == INF) { dijkstra(j.first); } } } } int main() { cin>>n; for(int i=2; i<n+1;i++) { Length[i] = INF; } for(int i=0; i<10; i++) { cin>>time[i]; } for(int i=0; i<n; i++) { cin>>alpha; nega_number[i] = alpha; number[alpha] = i+1; } for(int i=0; i<n; i++) { alpha = nega_number[i]; finding(alpha); } dijkstra(1); if(Length[n] == INF) { cout<<"-1"; } else { gamma = n; final_trace.push_back(gamma); cout<<Length[n]<<endl; while(gamma != 1) { gamma = trace[gamma]; final_trace.push_back(gamma); } cout<<final_trace.size()<<endl; for(int i=final_trace.size()-1;i>-1;i--) { cout<<final_trace[i]<<" "; } } //system("pause"); return 0; }修改当中的dijkstra

优化finding函数,#include<algorithm> #include<iostream> #include<vector> #include<string> #include<cmath> #include <cstdio> #include <map> #include <unordered_map> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; int n, gamma, time_count=0; int time[10]; string alpha; vector<int> Length(50005, 0); unordered_map<string, int> number; unordered_map<int, string> nega_number; vector<unordered_map<int, int>> edge(50005); vector<int> trace(50005, 0); vector<int> final_trace; void finding(string alpha) { int a=number[alpha], b; char beta; string epsilon; for(int i=9; i>=0; i--) { for(int j=1; j<10; j++) { epsilon = alpha; epsilon[i] = '0' + (int(epsilon[i]) + j) % 10; if(number.find(epsilon) != number.end() and epsilon != alpha) { b = number[epsilon]; edge[a][b]= time[i]; } } for(int j=i-1; j>=0; j--) { epsilon = alpha; beta = epsilon[j]; epsilon[j] = epsilon[i]; epsilon[i] = beta; if(number.find(epsilon) != number.end() and epsilon != alpha) { b = number[epsilon]; edge[a][b]= time[j]; } } } } void dijkstra(int i) { priority_queue, vector>, greater>> q; vector<bool> vis(n+1, false); q.push({0, i}); Length[i] = 0; while(!q.empty()) { int u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = true; for(auto j : edge[u]) { int v = j.first, w = j.second; if(Length[v] > Length[u] + w) { Length[v] = Length[u] + w; trace[v] = u; q.push({Length[v], v}); } } } } int main() { cin>>n; for(int i=2; i<n+1;i++) { Length[i] = INF; } for(int i=0; i<10; i++) { cin>>time[i]; } for(int i=0; i<n; i++) { cin>>alpha; nega_number[i] = alpha; number[alpha] = i+1; } for(int i=0; i<n; i++) { alpha = nega_number[i]; finding(alpha); } dijkstra(1); if(Length[n] == INF) { cout<<"-1"; } else { gamma = n; final_trace.push_back(gamma); cout<<Length[n]<<endl; while(gamma != 1) { gamma = trace[gamma]; final_trace.push_back(gamma); } cout<<final_trace.size()<<endl; for(int i=final_trace.size()-1;i>-1;i--) { cout<<final_trace[i]<<" "; } } system("pause"); return 0; }

最新推荐

recommend-type

基于Android 7.0与Android Studio的安卓学习.zip

Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件开发商及电信营运商共同研发改良。 Android操作系统的特点包括: 开放源代码:Android系统采用开放源代码模式,允许开发者自由访问、修改和定制操作系统,这促进了技术的创新和发展,使得Android系统具有高度的灵活性和可定制性。 多任务处理:Android允许用户同时运行多个应用程序,并且可以轻松地在不同应用程序之间切换,提高了效率和便利性。 丰富的应用生态系统:Android系统拥有庞大的应用程序生态系统,用户可以从Google Play商店或其他第三方应用市场下载和安装各种各样的应用程序,满足各种需求。 可定制性:Android操作系统可以根据用户的个人喜好进行定制,用户可以更改主题、小部件和图标等,以使其界面更符合个人风格和偏好。 多种设备支持:Android操作系统可以运行在多种不同类型的设备上,包括手机、平板电脑、智能电视、汽车导航系统等。 此外,Android系统还有一些常见的问题,如应用崩溃、电池耗电过快、Wi-Fi连接问题、存储空间不足、更新问题等。针对这些问题,用户可以尝试一些基本的解决方法,如清除应用缓存和数据、降低屏幕亮度、关闭没有使用的连接和传感器、限制后台运行的应用、删除不需要的文件和应用等。 随着Android系统的不断发展,其功能和性能也在不断提升。例如,最新的Android版本引入了更多的安全性和隐私保护功能,以及更流畅的用户界面和更强大的性能。此外,Android系统也在不断探索新的应用场景,如智能家居、虚拟现实、人工智能等领域。 总之,Android系统是一种功能强大、灵活可定制、拥有丰富应用生态系统的移动操作系统,在全球范围内拥有广泛的用户基础。
recommend-type

node-v4.6.1-sunos-x86.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

输出这段Python代码输出所有3位整数中,个位是5且是3的倍数的整数

``` for i in range(100,1000): if i%10 == 5 and i%3 == 0: print(i) ``` 输出结果: ``` 105 135 165 195 225 255 285 315 345 375 405 435 465 495 525 555 585 615 645 675 705 735 765 795 825 855 885 915 945 975 ```