Problem Archive #1 题解2

  接着上一次的题解接着写

  E题,水题,The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with xx steps, she will pronounce the numbers 1,2,…,x1,2,…,x in that order

  读有几个1就有几层,然后记录1前面的数字

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <set>
 6 using namespace std;
 7
 8 int main()
 9 {
10     int n;
11     cin>>n;
12     int num[1000],a[1000];
13     int t=0;
14     for(int i=0;i<n;i++){
15         cin>>num[i];
16         if(i>0&&num[i]==1) a[t++]=num[i-1];
17     }
18     cout<<t+1<<endl;
19     for(int i=0;i<t;i++){
20         cout<<a[i]<<‘ ‘;
21     }
22     cout<<num[n-1]<<endl;
23     return 0;
24 }

  F题,挺简单的一道题,只是需要模拟,一开始想错了,模拟操作了。TLE了1发

  题意是给两个字符串,每次只能删除两个字符串最前面的一个字符,看什么时候两个字符串相等,字符串可以为空

  先贴一份TLE的代码

  

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
using namespace std;

int main()
{
    string a,b;
    int sum=0;
    cin>>a>>b;
    while(a!=b){
        if(a.length()>b.length()){
            a=a.substr(1,a.length()-1);
            sum++;
        }
        else if(a.length()<b.length()){
            b=b.substr(1,b.length()-1);
            sum++;
        }
        else{
            a=a.substr(1,a.length()-1);
            b=b.substr(1,b.length()-1);
            sum+=2;
        }
    }
    cout<<sum<<endl;
    return 0;
}

  因为每次都要进行字符串截取的操作,所以就会TLE了

  只需要从后向前比较是否元素相同就行了,如果不同就break;

  最后附上ac的代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
using namespace std;

int main()
{
    string a,b;
    int sum=0;
    cin>>a>>b;
    if(a==b){
        cout<<0<<endl;
        return 0;
    }
    int q=a.length()-1;
    int w=b.length()-1;
    while(a[q]==b[w]){
        q--;
        w--;
    }
    sum+=w;
    sum+=q;
    sum+=2;
    cout<<sum<<endl;
    return 0;
}

  G题,大意就是给一串序列,然后对每一个数都进行一次判断,如果这串序列中不存在于这个数求和为2的次方的数,则删除这个数,问需要删除多少个数。

  思路就是,先打一个表,存2的次方,然后用map存这个序列,然后对序列中的每一个数与2的次方作差,看相减之后的数是否在map里

  

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <cstring>
using namespace std;
int main()
{
    int n,m;
    long long two[35];
    long long num[120000];
    map<int,int>s;
    two[0]=1;
    for(int i=1;i<35;i++) two[i]=2*two[i-1];
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>num[i];
        s[num[i]]++;
    }
    int sum=0;
    for(int i=0;i<n;i++){
        int t=0;
        for(int j=0;j<35;j++){
            if(num[i]<two[j]){
                if(s[two[j]-num[i]]!=0){
                        if(2*num[i]==two[j]){
                            if(s[two[j]-num[i]]>1){
                                t=1;
                                break;
                            }
                        }
                        else{
                            t=1;
                            break;
                        }

                }
            }
        }
        if(t==0) sum++;
    }
    cout<<sum<<endl;
    return 0;
}

H题,模拟题,给一串由数字组成的字符串看最大能划分成多少份能被三整除的数。

就是一位一位的判断,如果该位能被三整除,就划分,否则就需要看前一位的情况,然后分情况,分情况。。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <set>
 6 #include <string>
 7 using namespace std;
 8
 9 int main()
10 {
11     string s;
12     cin>>s;
13     int sum=0;
14     int t=0;
15     for(int i=0;i<s.length();i++)
16     {
17       int num=s[i]-‘0‘;
18       if(num%3==0){
19         sum++;
20         t=0;
21       }
22       else if(num%3==1)
23       {
24           if(t==0) t=1;
25           else if(t==1){
26             if(i!=s.length()-1){
27                 sum++;
28                 t=0;
29                 i++;
30             }
31           }
32           else{
33             sum++;
34             t=0;
35         }
36       }
37       else
38       {
39           if(t==0) t=2;
40           else if(t==2){
41             if(i!=s.length()-1){
42                 sum++;
43                 t=0;
44                 i++;
45             }
46         }
47           else{
48             sum++;
49             t=0;
50
51         }
52       }
53     }
54     cout<<sum<<endl;
55     return 0;
56 }

原文地址:https://www.cnblogs.com/maybe96/p/9384737.html

时间: 2024-08-03 00:18:42

Problem Archive #1 题解2的相关文章

BZOJ3212 Pku3468 A Simple Problem with Integers 题解

题目大意: 一个数列,有两个操作:1.修改操作,将一段区间内的数加上c:2.查询操作,查询一段区间内的数的和. 思路: 线段树裸题,区间修改.区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 6 long long sum[400001],mor[4

acm_hdu Problem Archive 1002

#include <stdio.h>#include <stdlib.h>#include <string.h> void add(char *a, char *b,char *sum)//字符串a要比字符串b长,或相等{ int len_a = strlen(a); int len_b = strlen(b); char tmp[len_a]; int offset = len_a-len_b; strncpy(tmp+offset,b,len_b);//后面len_

acm_hdu Problem Archive 1004

#include <stdio.h>#include <stdlib.h>#include <string.h> int main(){ int n = 0; while (scanf("%d",&n) != EOF && n!= 0) { char str[1000][16];//只对各不相同的颜色进行存储 memset(str,0,sizeof(str));//all set to be NUL char tmp[16];

poj 3468 A Simple Problem with Integers(原来是一道简单的线段树区间修改用来练练splay)

题目链接:http://poj.org/problem?id=3468 题解:splay功能比线段树强大当然代价就是有些操作比线段树慢,这题用splay实现的比线段树慢上一倍.线段树用lazy标记差不多要2s用splay要4s.可以用splay来实现线段树的区间操作更深层次的了解一下splay算是入个门. #include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #i

2017ACM省赛选拔赛题解

Problem A: 聪明的田鼠 题解: dp[k][i]表示走了k步,且在第i行的最大值 最后的结果就是走了n+m-2步,且在第n行的值 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <cstdio> 7 #include <string> 8 #inclu

2017 CCPC杭州 题解

2017CCPC杭州题目PDF Problem A. Super-palindrome 题解: 给你一个字符串,每一步可以将一个字符替换为另一个字符,问你最少多少步可以使得,该字符串任意奇数子串为回文串,偶数子串为回文串. 满足上面条件一定是ababab这种形式,所以我们只要找到数量最多的两种字符用n-numa-numb得到ans1,有可能一种字符的数量过多,这时候我们只要把所有字符都变成这种字符就行了.得到n-numa,ans2; 在ans1和ans2中去最小值就是答案了: 参考代码: #in

[C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的“Problem Archive”,然后从众多题目中选择一个进行读题.构思.编程.然后提交.最后查看题解状态,如果AC了表示这一题被攻克了,否则就要重做了~一般情况下,“刷题”要求精神高度集中且经验丰富,否则很难成功AC,有时候甚至做一题要浪费半天的时间!(有时网速卡了,比抢火车票还要急!) 楼主在

poj 1141 区间dp

题目链接:http://poj.org/problem?id=1141 题解:略 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define ll long long const int maxn=1e2+5; const int INF=0x3f3f3f3f; int dp[105][105]; int

【HDU3949】XOR

[题目大意] 给定一个数组,求这些数组通过异或能得到的数中的第k小是多少. 传送门:http://vjudge.net/problem/HDU-3949 [题解] 首先高斯消元求出线性基,然后将k按照二进制拆分即可. 注意当高斯消元结束后若末尾有0则第1小是0 特判一下然后k--. 然后HDU输出long long是用%I64d 无论C++还是G++都是.(虽然我用了lld也AC了) 1 #include<iostream> 2 #include<cstdio> 3 #includ