hdu 5676(stl 或 深搜)

学习:

  字符串的下一个排列,字符串的大小比较。strcmp;

stl:

  

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char c[50],b[50];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",b);
        int len = strlen(b);

        if(len & 1){
            len = (len + 1) / 2;
            for(int i = 0 ; i < len ; i ++) printf("4");
            for(int i = len ; i < 2 * len ; i ++) printf("7");
            printf("\n");
        }else{
            memset(c,0,sizeof(c));
            for(int i = 0 ; i < len / 2 ; i ++) c[i] = ‘4‘;
            for(int i = len / 2 ; i < len ; i ++) c[i] = ‘7‘;
            bool f = true;
            do{
                if(strcmp(c,b) >= 0){
                    f = false;
                    printf("%s\n",c);
                    break;
                }
            }while(next_permutation(c,c+len));
            if(f){
                for(int i = 0 ; i < (len+2) / 2 ; i ++) printf("4");
                for(int i = (len+2) / 2 ; i < len+2 ; i ++) printf("7");
                printf("\n");
            }
        }
    }
    return 0;
}

按位dfs: 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4
 5 ll n;
 6 bool f;
 7 int calc(ll x){
 8     int cnt = 0;
 9     while(x){
10         cnt ++;
11         x /= 10;
12     }
13     return cnt;
14 }
15
16 void dfs(ll now,int lx,int ly){
17     if(f) return;
18  //   cout << "now :" << now << "   " << lx << "   "  << ly << endl;
19     if(now >= n && lx == 0 && ly == 0){
20         printf("%lld\n",now);
21         f = true;
22         return;
23     }
24     if(lx) dfs(now*10+4,lx-1,ly);
25     if(ly) dfs(now*10+7,lx,ly-1);
26     return ;
27 }
28
29 int main(){
30
31     int T;
32     scanf("%d",&T);
33     while(T--){
34         scanf("%lld",&n);
35         int len = calc(n);
36         if(len & 1) len ++;
37         if(!n) printf("47\n");
38         if(!n) continue;
39         if(len >= 20){
40             for(int i = 0 ; i < len/2 ; i ++) printf("4");
41             for(int i = 0 ; i < len/2 ; i ++) printf("7");
42             printf("\n");
43         }else{
44             f = false;
45             dfs(0,len >> 1,len >> 1);           // 4和7数量相等
46             if(!f){
47                 for(int i = 0 ; i < (len+2)/2 ; i ++) printf("4");
48                 for(int i = 0 ; i < (len+2)/2 ; i ++) printf("7");
49                 printf("\n");
50             }
51         }
52     }
53     return 0;
54 }
时间: 2025-01-09 17:17:40

hdu 5676(stl 或 深搜)的相关文章

hdu 1175 连连看 (深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以消掉.还有一个要注意的地方的就是转弯.转弯的次数不超过两次,这两个棋子才可以在棋盘上消去~ 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int

hdu 1518 Square(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 --------------------------------------------------------------------------------------------------------------------------------------------

hdu 1518 Square(深搜+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! 1 #include <iostream> 2 #include <cstdio> 3 #include<algorithm> 4 #include <cstring> 5 using namespace std; 6 int ap[30],visit[30]

HDU 1036 Robot Motion 深搜

 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (up the page) S south (down the page) E east (t

hdu 1342 记忆化深搜

Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1657    Accepted Submission(s): 811 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49

HDU 5325 Crazy Bobo(深搜)

题意:给一个n,接着输入n个数,表示n个点的值,接着输入n-1条边形成一个生成树 问最大有多少个点的集合使得该集合内的所有点都满足如下:对于集合内点大小相邻的两个点,该两点之间经过的所有点的大小都小于该两点 eg  7 3 30 350 100 200 300 400 1 2 2 3 3 4 4 5 5 6 6 7 该无向图可表示为1--2--3--4--5--6--7   取点6对于集合(3-7)来说,正好比他大的点为点3,且他们之间的所有点的 大小都小于该两点,满足条件   同理取该集合内的

HDU 5305 Friends (深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题面: Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1149    Accepted Submission(s): 569 Problem Description There are n people and m

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

深搜基础题目 杭电 HDU 1241

HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #include <iostream> using namespace std; char land[150][150]; int p,q; void dfs(int x,int y){ land[x][y] = '*'; if(land[x-1][y]!= '@' && land[x+1]