Swust 2019级第5次周赛-赛后总结及反思

A-CF1167A
终于看到签到题了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=1e6+50;
const int inf=0x3f3f3f3f;
const int MOD=10007;

char s[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        scanf("%s",s);
        int sign=1;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='8')
            {
                if(n-i>=11)
                {
                    printf("YES\n");
                    sign=0;
                    break;
                }
            }
        }
        if(sign) printf("NO\n");
    }
    return 0;
}

B-HDU4054
字符串处理

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=1e4+50;
const int inf=0x3f3f3f3f;
const int MOD=10007;

int i;
int main()
{
    char s[maxn];
    while (gets(s) != NULL)
    {
        int len=strlen(s);
        i=0;
        for(;i+16<len;i+=16)
        {
            printf("%04x:",i);
            for(int j=i;j<i+16;j+=2)
            {
                printf(" %x%x",s[j],s[j+1]);
            }
            printf(" ");
            for(int j=i;j<i+16;j++)
            {
                if(s[j]<='z'&&s[j]>='a') printf("%c",s[j]-32);
                else if(s[j]<='Z'&&s[j]>='A') printf("%c",s[j]+32);
                else printf("%c",s[j]);
            }
            printf("\n");
        }
        if(i+16>=len)//开始没写等于WA了一次,不写等于的话碰到16的倍数会没有输出
        {
            printf("%04x:",i);
            int tmp=len-i;
            int tmp2=i+16-len;
            for(int j=i;j<i+tmp;j++)
            {
                if(j%2==0) printf(" %x",s[j]);
                else printf("%x",s[j]);
            }
            for(int j=i+tmp;j<i+16;j++)
            {
                if(j%2==0) printf("  ");
                else printf("   ");
            }
            if(tmp2%2==0) printf(" ");
            for(int j=i;j<i+tmp;j++)
            {
                if(s[j]<='z'&&s[j]>='a') printf("%c",s[j]-32);
                else if(s[j]<='Z'&&s[j]>='A') printf("%c",s[j]+32);
                else printf("%c",s[j]);
            }
            printf("\n");
        }
    }
    return 0;
}

C-CF231A
第二个签到题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=1e6+50;
const int inf=0x3f3f3f3f;
const int MOD=10007;

char s[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    int ans=0;
    while(t--)
    {
        int a[4];
        for(int i=1;i<=3;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+4);
        if(a[2]==1) ans++;
    }
    printf("%d\n",ans);
    return 0;
}

D-hihocoder1184
看见数据范围在int范围内,考虑到2^32>1e9,最坏的情况下运算次数也不可能太大
循环遍历9—>2 ,用N去除,记录9—2使用次数,从小到大输出即可,
ps:开始想着对记录的次数用ans=ans*10+i,输出ans WA了4次 位数过大爆int了 Orz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=1e6+50;
const int inf=0x3f3f3f3f;
const int MOD=10007;

int a[11];
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        memset(a,0,sizeof(a));
        if(t==1)
        {
            printf("1\n");
            continue;
        }
        int sign=0;
        while(t>1)
        {
            for(int i=9;i>=2;i--)
            {
                if(t%i==0)
                {
                    a[i]++;
                    t/=i;
                    break;
                }
                else if(i==2) sign=1;
            }
            if(sign) {printf("-1\n");break;}
        }
        int ans=0;
        if(!sign)
        {
            for(int i=2;i<=9;i++)
            {
                while(a[i])
                {
                    printf("%d",i);
                    a[i]--;
                }
            }
            printf("\n");
        }
    }
    return 0;
}

E-SCU4438
以下全是废话
开始想着用kmp中嵌套,不出意料的T了,然后想着对kmp进行优化,每删除一次,就考虑删除位置往前 n(删除的长度,大概,推出来的具体值给忘了 )的位置再继续kmp,很可惜还是T了QAQ
赛后看题解有看到用kmp过这道题的,我就想到是不是string类型过于耗时,
最后得到学长的回复是string的erase均摊的复杂度为O(N^2),并且提醒我们不要随便用不清楚复杂度的STL。。。

学长给的hash解

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 10007;
const int N = 1000010;
ull hash1[N * 5], p[N * 5];
char txt[N * 5], str[N * 5];
char st[N * 5];
/**
 * 首先求出模式串的哈希值
 * 考虑类似于一个stack的数组,把文本串依次加入数组,
 * 如果加入一个字符后出现了模式串,就把模式串相同的子串弹出。
 * 使用字符串哈希可以O(1)判断,总时间复杂度O(n)
 * 
 * 本题还可以用kmp算法,时间与内存均略微优于字符串哈希,请下来自己思考。
 */
int main() {
    p[0] = 1;
    for (int i = 1; i < N * 5; i++) p[i] = p[i - 1] * 131;
    while (~scanf("%s%s", str, txt)) {
        int tail = 0;
        int len1 = strlen(str);
        int len2 = strlen(txt);
        ull tmp = str[0];
        for (int i = 1; i < len1; i++)
            tmp = tmp * 131 + str[i];
        hash1[0] = 0;
        int po = 0;
        for (int i = 0; i < len2; i++) {
            st[tail++] = txt[i];
            po++;
            hash1[po] = hash1[po - 1] * 131 + txt[i];
            if (po < len1) continue;
            ull tt = hash1[po] - hash1[po - len1] * p[len1];
            if (tt == tmp) {
                int cnt = 0;
                while (cnt < len1) {
                    tail--;
                    cnt++;
                    po--;
                }
            }
        }
        for (int i = 0; i < tail; i++) printf("%c", st[i]);
        printf("\n");
    }
    return 0;
}

kmp解
也是构造一个栈,比较的时候将字符压进栈中,匹配到的模式串压出,再继续往下比较,最后栈中剩下的就是答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=5e6+50;
const int inf=0x3f3f3f3f;
const int MOD=10007;

char match[maxn];
char be_match[maxn];
int len_b;
int len_m;
int Next[maxn];
char ans[maxn];
struct node
{
    int x;//字符与模式串匹配到的位置
    char c;
    node(int a,char b):x(a),c(b){};
};

void get_next()
{
    int j=Next[0]=-1;
    for(int i=1;i<len_m;i++)
    {
        while(j!=-1&&match[i]!=match[j+1]) j=Next[j];
        if(match[i]==match[j+1]) j++;
        Next[i]=j;
    }
}

void kmp()
{
    stack <node> st;
    len_b=strlen(be_match);
    for(int i=0,j=-1;i<len_b;i++)
    {
        while(j!=-1&&match[j+1]!=be_match[i]) j=Next[j];
        if(match[j+1]==be_match[i])
        {
            j++;
        }
        st.push(node(j,be_match[i]));//压入栈中
        if(j+1==len_m)
        {
            int k=len_m;
            while(k--) st.pop();
            if(st.empty()) j=-1;
            else j=st.top().x;
        }
    }
    int k=0;
    while(!st.empty())
        ans[k++]=st.top().c,st.pop();
    for(int i=k-1;i>=0;i--) printf("%c",ans[i]);
    printf("\n");
}

int main()
{
    while(scanf("%s%s",match,be_match)!=EOF)
    {
        len_m=strlen(match);
        get_next();
        kmp();
    }
    return 0;
}

F-POJ2774
学长标程

//题意:求两个字符串的最长公共字串的长度
//思路:二分+字符串hash
//先二分答案x,然后判断是否存在长度为x的相同子串
//---预处理出s中长度为x的子串的哈希值并排序,枚举t所有长度为x的子串判断是否在s中也存在
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define uLL unsigned long long
const int mod=1e9+7;
const int manx=1e5+10;
const int base=131;

char s[manx],t[manx];
int n,m;
uLL hs[manx],ht[manx],mul[manx],a[manx];
int check(int len)
{
    int cou=0;
    for(int i=len;i<=n;i++)//存下s中 以i结尾长度为len的子串 的hash值
        a[cou++]=hs[i]-hs[i-len]*mul[len];
    sort(a,a+n);//二分查找需要先排序
    for(int i=len;i<=m;i++)
    {
        uLL temp=ht[i]-ht[i-len]*mul[len];
        if(binary_search(a,a+n,temp))return 1;
    }
    return 0;
}
int main()
{
    scanf("%s%s",s+1,t+1);
    n=strlen(s+1),m=strlen(t+1);
    hs[0]=ht[0]=0;
    mul[0]=1;
    for(int i=1;i<=n;i++)hs[i]=hs[i-1]*base+s[i];
    for(int i=1;i<=m;i++)ht[i]=ht[i-1]*base+t[i];
    for(int i=1;i<=max(n,m);i++)mul[i]=mul[i-1]*base;
    int l=0,r=min(n,m);
    int ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    printf("%d\n",ans);;
    return 0;
}

第一次A了这么多题,要感谢学长不杀之恩
细节之类的问题还是要多注意,然后就是STL的使用要谨慎…

赛况记录
A-AC
B-WA1次(没写等于)
C-AC
D-WA4次(爆int)
E-未完成
F-未完成

contest链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值