本文主要是介绍从文件读取字符串,并求出该字符串包含的字符集中所有字符的全部组合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
求给定字符集中所有字符的全部组合。首先从文件“data.txt”中读取字符集,并识别出字符集中的每一个字符。假设字符集的总字符数为n,利用这个n个字符组成长为n的字符串。其中每个字符均可重复使用,每个字符串中也不必包含全部字符。试求出所有长为n的字符串集合。最后将所求的的集合写入文件“result.txt”中。
程序输入输出样例
– data.txt中的内容为
@#
– result.txt中的内容为
@@
@#
#@
##
枚举 递归 卡了半天,纯粹的弱智不解释。。。。。
//
// main.cpp
// str
//
// Created by Bryan on 14-6-28.
// Copyright (c) 2014年 Bryan. All rights reserved.
//#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <list>using namespace std;list<char> GetCharsFromFile()
{ifstream inFile("data.txt");list<char> ret;if(inFile){string s;getline(inFile,s);for(int i = 0;i<s.length();i++){ret.push_back(s[i]);}ret.sort();ret.unique();inFile.close();return ret;}return ret;
}void CharCombination(vector<string> &strVec,list<char> &charList,char *s,int step)
{if(step == charList.size()){s[step] = '\0';string stmp(s);strVec.push_back(stmp);return;}for(list<char>::iterator it = charList.begin();it != charList.end();it++){s[step] = *it;CharCombination(strVec,charList ,s, step + 1);}}int main(int argc, const char * argv[])
{list<char> charList;charList = GetCharsFromFile();vector<string> ret;char *cstr = new char[charList.size() + 1];CharCombination(ret,charList, cstr,0);ofstream outFile;outFile.open("result.txt");for(int i = 0;i<ret.size();i++)outFile<<ret[i]<<endl;outFile.close();return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>using namespace std;void func(vector<char> & s,char *ret,int k,int N,ofstream &outFile)
{if(k == N ){ret[N] = '\0';cout<<ret<<endl;outFile<<ret<<endl;return;}for(int i = 0;i<N;i++){ret[k] = s[i];func(s,ret,k+1,N,outFile);}
}
int main()
{ifstream inFileStream;inFileStream.open("data.txt");ofstream outFileStream;outFileStream.open("result.txt");char *str = new char[1000];inFileStream.getline(str,1000);vector<char> vec;for(int i = 0;i<1000;i++){if(str[i] != '\0')vec.push_back(str[i]);elsebreak;}int N = vec.size();char * ret = new char[N+1];func(vec,ret,0,N,outFileStream);inFileStream.close();outFileStream.close();getchar();}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>using namespace std;void func(const vector<char> &charVec,char* finalStr,ofstream & outFile,int k,int N)
{if(k==N){finalStr[N]= '\0';cout<<finalStr<<endl;outFile<<finalStr<<endl;return;}for(int i= 0;i<N;i++){finalStr[k] = charVec[i];func(charVec,finalStr,outFile,k+1,N);}
}int main()
{ofstream outFile;outFile.open("result.txt");ifstream inFile;inFile.open("data.txt");string inStr;vector<char> charVec;outFile<<"The chars are: ";cout<<"The chars are: ";while(getline(inFile,inStr)){for(string::iterator it = inStr.begin();it != inStr.end();it++){cout<<(*it)<<" ";outFile<<(*it)<<" ";charVec.push_back(*it);}}cout<<endl;int N = charVec.size();char* outStr =new char[N+1];func(charVec,outStr,outFile,0,N);outFile.close();inFile.close();getchar();
}
这篇关于从文件读取字符串,并求出该字符串包含的字符集中所有字符的全部组合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!