解题思路:
注意事项:
参考代码:
#include<bits/stdc++.h> using namespace std; map<char,int>m; //对排列中的字符进行查重 set<string>Set_; //对字符串排列进行查重 string s=""; int ans=0; int num; void dfs(int index){ if(index>=num&&Set_.count(s)==0){ Set_.insert(s); //cout<<s<<endl; ans++; return; } for(auto i=m.begin();i!=m.end();i++){ //对map中出现过的字符进行遍历 if(i->second<=0){ //如果字符出现次数为0,跳过这次循环 continue; } s+=i->first; i->second--; //将字符出现的次数减去1 dfs(index+1); //进入下一层搜索 s.erase(s.length()-1); //删除叶子节点元素 i->second++; } } main(){ string temp; cin>>num; cin>>temp; for(int i=0;i<num;i++){ m[temp[i]]++; //用map统计字符 } dfs(0); cout<<ans; }
0.0分
1 人评分
C语言训练-求1+2!+3!+...+N!的和 (C++代码)浏览:1172 |
C二级辅导-同因查找 (C++代码)(42的倍数,,所以直接递加42输出)浏览:1097 |
点我有惊喜!你懂得!浏览:4109 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:704 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:580 |
模拟计算器 (C语言代码)浏览:892 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:520 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5232 |
WU-拆分位数 (C++代码)浏览:785 |
【矩阵】 (C++代码)浏览:950 |