字典升序问题



 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  }

 

     

时间: 2024-08-30 12:50:41

字典升序问题的相关文章

java参数,字典升序排列

开发的这段时间,遇到过不同的一些接口,有些接口比较严谨,需要参数字典升序进行加密,因此编写了以下这个方法进行排序,采用存入map的方式,对他的key进行字典升序排列,返回有顺序的list. public static List getCode(Map map){ List list = new ArrayList(); Iterator iter = map.entrySet().iterator(); //获得map的Iterator while(iter.hasNext()) { Entry

php 字典升序从小到大方法

/* 字典升序*/ function formatParaMap($paraMap) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { $buff .= $k . "=" . $v . "&"; } $reqPar = ''; //去掉最后一个字符 $ if (strlen($buff) > 0) { $reqPar = substr($buff,

微信H5支付开发全过程(除内置微信以外的浏览器)

前言:网上都是~ 呵呵 自己搞 只要花时间 多问客服总会有的 只说程序不说准备工作 自己ID 或者秘钥都准备好了  写的有点儿乱 可以把所有的方法 放在一个文件中调用 public function wechat ($doorder,$doczmoney,$doczbody) { // H5 微信支付 $wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //下单接口 $appid = ""; //公众

poj 1833

http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排列无后继的最大值时返回值为false,其他的为true: 也可以在其后加一个cmp函数 1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 5 using namespace std; 6

next_permutation函数

转自此处 http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类型的next_permutation int main(){ int a[3];a[0]=1;a[1]=2;a[2]=3; do{cout<<a[0]<<" &

【宿舍菜鸟们的ACM解题笔记】487-3279

题目来源 北大ACM,题目ID 1002,难度 初级. 题目简介 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dia

487--3279 UVA 755 其实有三种解法

说说:这道题初看挺简单的,题意无非就是将一串字符转化成一个用'-'隔开的电话号码,然后把出现超过一次的号码按照字典升序输出即可.但是这样样做是会超时的....其实把电话号码中间的'-'去掉,电话号码其实就是一个整数,有了这个想法那就简单啦.只要设立一个超大的数组包含所有可能的电话号码,然后数组的值是该号码出现的次数,统计完后遍历一遍输出即可.但是第三种相当于把前两种方法法结合起来了.把号码当成一个整数,但是号码存储在一个数组中,号码出现的次数存储在另一个数组中.这样在插入新号码的时候就排序,然后

PKU1002 487-3279

英文: Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP.

prev_permutation 函数

这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 下面是以前的笔记    与之完全相反的函数还有prev_permutation (1) int 类型的next_permutation int main() { int a[3]; a[0]=1;a[1]=2;a[2]=3; do { cout<<a[0]<<" "<<a[1]<<" "<<a[2]<