标签:
1 // 字典升序问题 2 #include <iostream> // std::cout 3 #include <iterator> // std::advance 4 #include <list> // std::list 5 #include <cmath> 6 using namespace std; 7 8 int f(int i,int k){ //定义:返回以第i个字母打头的k位字符串总个数 9 if(k==1) return 1; 10 int sum=0; 11 for(int j=i+1;j<=26;++j) 12 { 13 sum+=f(j,k-1); } 14 return sum; 15 16 } 17 18 int g(int k){//定义:返回k位字符串总个数 19 int sum1=0; 20 for(int i=1;i<=26;++i){ 21 sum1+=f(i,k); 22 } 23 return sum1; 24 } 25 26 27 int main(){ 28 string a;//string的成员函数 29 cin>>a; 30 31 for(int i=0;i<a.size()-1;++i) 32 if(a[i]>=a[i+1]) 33 { 34 printf ("The string must be in ascending order,please check your string!"); 35 system("PAUSE"); 36 exit (EXIT_FAILURE); 37 } 38 39 int sum0=0; 40 int k=a.size(); 41 for(int i=1;i<=k-1;++i) 42 sum0+=g(i); 43 44 // cout<<sum0<<endl; 45 if(k>1){ 46 for(int i=1;i<=a[0]-‘a‘;++i) 47 sum0+=f(i,a.size()); 48 //cout<<sum0<<endl; 49 //cout<<g(1)+g(2)+g(3)+f(1,4)+f(2,4)+f(3,4); 50 } 51 //cout<<sum0<<endl; 52 53 k--; 54 while(k>1){ 55 int j=a[a.size()-k-1]-‘a‘+1;//提取当前研究位 56 int jj=j+1; 57 int jjj=a[a.size()-k]-‘a‘; 58 59 for(int i=jj;i<=jjj;++i) 60 sum0+= f(i,k); 61 k--; 62 } 63 if(a.size()==1) sum0=a[0]-‘a‘+1; 64 else 65 sum0+=a[a.size()-1]-a[a.size()-2]; 66 cout<<sum0<<endl; 67 68 69 // cout<<sum0<<endl; 70 71 //cout<<f(1,2)<<endl; 72 // cout<<g(6)<<endl; 73 system("PAUSE"); 74 return 1; 75 76 77 }
标签:
原文地址:http://www.cnblogs.com/jieforever/p/4679880.html