字典序问题

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int ff(int ii,int k)
  5. {
  6.     int sum = 0;
  7.     if( k <= 1) return 1;
  8.     else{
  9.             for(int m = ii+1; m <= 26; m++)
  10.             {
  11.                 sum += ff(m,k-1);
  12.             }
  13.             return sum;
  14.         }
  15. }
  16. int gg(int k)
  17. {
  18.     int ss = 0;
  19.     for(int jj = 1; jj <= 26; jj++)
  20.     {
  21.         ss += ff(jj,k);
  22.     }
  23.     return ss;
  24. }
  25. int main()
  26. {
  27.     int pc = 0;
  28.     int caseNum = 1;
  29.     
  30.     //cin>>caseNum;
  31.     
  32.     int *pResult = new int [caseNum];
  33.     
  34.     for(int i = 0; i < caseNum; i++)
  35.         pResult[i] = 0;
  36.     while( pc < caseNum )
  37.     {
  38.         int no = 0;
  39.         int len = 0;
  40.         string str = "";
  41.         cin>>str;
  42.         len = str.size();
  43.         const char* pstr = str.c_str();
  44.         if( len <= 1 )
  45.         {
  46.             pResult[pc] = int(*(pstr)) - 96;
  47.         }
  48.         else{
  49.                 int oldNo;
  50.                 if( *(pstr) == 1)   oldNo = 1;
  51.                 else{
  52.                     oldNo = 0;
  53.                 }
  54.                 for(int i = 1; i <= len; i++)
  55.                 {
  56.                     no = int(*(pstr+i-1)) - 96;
  57.     
  58.                     for(int j = oldNo+1; j < no; j++)
  59.                     {
  60.                         pResult[pc] += ff(j,len-i+1); 
  61.                     }
  62.                     
  63.                     oldNo = no;
  64.                 }
  65.                 pResult[pc] += 1;
  66.                 
  67.                 for(int i = 1; i <= len-1; i++)
  68.                 {
  69.                     pResult[pc] += gg(i);
  70.                 }
  71.             }
  72.         pc++;
  73.     }
  74.     for(int i = 0; i < caseNum; i++)
  75.         cout<<pResult[i]<<endl;
  76.     delete []pResult;
  77.     return 0;
  78. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值