Atcoder CODE FESTIVAL 2017 qual C 总结+ABCD题解

来源:互联网 发布:淘宝客api怎么用 编辑:程序博客网 时间:2024/06/06 00:27

    这场比赛..打得还不错..最后两题我想不出..不过还好过了的人也不怎么多。所以我以65分钟过了4题的成绩排在了rank167.(顺带Orz orbitingflea)最终加了42,rating:2026(终于黄名了qwq)..朝着下一个目标进发!
    以下是题解(ABCD)
=================我是萌萌哒分割线OvO=================

A - Can you get AC?

Problem Statement

    Snuke built an online judge to hold a programming contest.
    When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string S as a contiguous substring. (The judge can return any two-character substring of S.)
    Determine whether the judge can return the string “AC” as the verdict to a program.

Constraints

    2≤|S|≤5
    S consists of uppercase English letters.

Input

    Input is given from Standard Input in the following format:
        S

Output

    If the judge can return the string “AC” as a verdict to a program, print “Yes”; if it cannot, print “No”.

Examples

Example 1
    Input
        BACD
    Output
        Yes
    The string “AC” appears in “BACD” as a contiguous substring (the second and third characters).
Example 2
    Input
        ABCD
    Output
        No
    Although the string “ABCD” contains both “A” and “C” (the first and third characters), the string “AC” does not appear in “ABCD” as a contiguous substring.
Example 3
    Input
        CABD
    Output
        No
Example 4
    Input
        ACACA
    Output
        Yes
Example 5
    Input
        XX
    Output
        No

题意

    给你一个字符串,问你串中是否包含子串“AC”。

思路

    大水题,扫过去判一下就好了..

Code

#pragma GCC optimize(3)#include<bits/stdc++.h>using namespace std;typedef long long ll;bool Finish_read;template<class T>inline void read(T &x) {    Finish_read=0;x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;Finish_read=1;}template<class T>inline void print(T x) {    if(x/10!=0)        print(x/10);    putchar(x%10+'0');}template<class T>inline void writeln(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);    putchar('\n');}template<class T>inline void write(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);}/*================Header Template==============*/string s;int main() {    cin>>s;    for(int i=0;i<s.length()-1;i++)        if(s[i]=='A'&&s[i+1]=='C') {            puts("Yes");            return 0;        }    puts("No");    return 0;}

B - Similar Arrays

Problem Statement

    We will say that two integer sequences of length N, x1,x2,…,xN and y1,y2,…,yN, are similar when |xiyi|≤1 holds for all i (1≤i≤N).
    In particular, any integer sequence is similar to itself.
    You are given an integer N and an integer sequence of length N, A1,A2,…,AN.
    How many integer sequences b1,b2,…,bN are there such that b1,b2,…,bN is similar to A and the product of all elements, b1b2bN, is even?

Constraints

    1≤N≤10
    1≤Ai≤100

Input

    Input is given from Standard Input in the following format:

N
A1 A2AN

Output

    Print the number of integer sequences that satisfy the condition.

Examples

Example 1
    Input
        2
        2 3
    Output
        7
    There are seven integer sequences that satisfy the condition:

1,2
1,4
2,2
2,3
2,4
3,2
3,4

Example 2
    Input
        3
        3 3 3
    Output
        26
Example 3
    Input
        1
        100
    Output
        1
Example 4
    Input
        10
        90 52 56 71 44 8 13 30 57 84
    Output
        58921

题意

    给你一个长度为n的数组,然后让你求与他相似的数组的个数,定义相似为两个数组长度相等且每一位的差的绝对值不超过1,并且数组所有数的乘积不是奇数

思路

    其实这题有O(n)做法,只是我看范围也不大,我就写了个DFS..没想到竟然没有T还只用了2ms..233333.对于DFS的话就是每一位扫3个数推下去就行了

Code

#pragma GCC optimize(3)#include<bits/stdc++.h>using namespace std;typedef long long ll;bool Finish_read;template<class T>inline void read(T &x) {    Finish_read=0;x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;Finish_read=1;}template<class T>inline void print(T x) {    if(x/10!=0)        print(x/10);    putchar(x%10+'0');}template<class T>inline void writeln(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);    putchar('\n');}template<class T>inline void write(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);}/*================Header Template==============*/int arr[15],a[15],n,ans=0;ll sum=1;inline void dfs(int pos) {    if(pos==n+1) {        sum=1;        for(int i=1;i<=n;i++)            sum*=a[i];        if(sum%2==0)            ans++;        return;    }    for(int i=arr[pos]-1;i<=arr[pos]+1;i++) {        a[pos]=i;        dfs(pos+1);    }}int main() {    read(n);    for(int i=1;i<=n;i++)        read(arr[i]);    dfs(1);    writeln(ans);}

C - Inserting ‘x’

Problem Statement

    We have a string s consisting of lowercase English letters. Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

    Snuke’s objective is to turn s into a palindrome. Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

    A palindrome is a string that reads the same forward and backward. For example, “a”, “aa”, “abba” and “abcba” are palindromes, while “ab”, “abab” and “abcda” are not.

Constraints

    1≤|s|≤105
    s consists of lowercase English letters.

Input

    Input is given from Standard Input in the following format:
        s

Output

    If the objective is achievable, print the number of operations required. If it is not, print “-1” instead.

Examples

Example 1
    Input
        xabxa
    Output
        2
    One solution is as follows (newly inserted ‘x’ are shown in bold):
    xabxa → xa x bxa → xaxbxa x
Example 2
    Input
        ab
    Output
        -1
    No sequence of operations can turn s into a palindrome.
Example 3
    Input
        a
    Output
        0
    s is a palindrome already at the beginning.
Example 4
    Input
        oxxx
    Output
        3
    One solution is as follows:
    oxxx → x oxxx → x xoxxx → x xxoxxx

题意

    给你一个字符串,问你能不能通过加最少的x来获得回文串。

思路

    首先,对于原串,我们将所有的x拿掉,进行一遍回文串判定,如果不是回文串,那么很显然直接输出-1,否则将原串用两个指针i,j分别从右往左和从左往右向中间扫。如果s[i]==s[j]那么直接i++,j–;否则如果s[i]==’x’,那么就ans++,i++;否则就是s[j]==’x’,那么就ans++,j–;最后扫到i>=j就行了。

Code

#pragma GCC optimize(3)#include<bits/stdc++.h>using namespace std;typedef long long ll;bool Finish_read;template<class T>inline void read(T &x) {    Finish_read=0;x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;Finish_read=1;}template<class T>inline void print(T x) {    if(x/10!=0)        print(x/10);    putchar(x%10+'0');}template<class T>inline void writeln(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);    putchar('\n');}template<class T>inline void write(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);}/*================Header Template==============*/string s,n="";int ans=0;int main() {    cin>>s;    for(int i=0;i<s.length();i++)        if(s[i]!='x')            n+=s[i];    for(int i=0,j=n.length()-1;i<=j;i++,j--)        if(n[i]!=n[j]) {            puts("-1");            return 0;        }    for(int i=0,j=s.length()-1;i<=j;) {        if(s[i]==s[j]) {            i++;            j--;            continue;        }        if(s[i]=='x') {            ans++;            i++;        }        if(s[j]=='x') {            ans++;            j--;        }    }    printf("%d\n",ans);}

D - Yet Another Palindrome Partitioning

Problem Statement

    We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s1, s2, …, sN from left to right. (Here, s=s1+s2+…+sN holds.) Snuke wants to satisfy the following condition:
    For each i (1≤i≤N), it is possible to permute the characters in si and obtain a palindrome.
    Find the minimum possible value of N when the partition satisfies the condition.

Constraints

    1≤|s|≤2×105
    s consists of lowercase English letters.

Input

    Input is given from Standard Input in the following format:
        s

Output

    Print the minimum possible value of N when the partition satisfies the condition.

Examples

Example 1
    Input
        aabxyyzz
    Output
        2
    The solution is to partition s as “aabxyyzz” = “aab” + “xyyzz”. Here, “aab” can be permuted to form a palindrome “aba”, and “xyyzz” can be permuted to form a palindrome “zyxyz”.
Example 2
    Input
        byebye
    Output
        1
    “byebye” can be permuted to form a palindrome “byeeyb”.
Example 3
    Input
        abcdefghijklmnopqrstuvwxyz
    Output
        26
Example 4
    Input
        abcabcxabcx
    Output
        3
    The solution is to partition s as “abcabcxabcx” = “a” + “b” + “cabcxabcx”.

题意

    给你一个字符串,让你将他分成最少的子串,使得每个子串重排列后都是一个回文串

思路

    考虑dp,f[i]表示到i这一位最少可以分成几个串。对于每一个位置,我们记录从字符串开头到该位置的所有字母的个数是单数还是双数,这样我们可以对于每一个位置就是一个26位的01串,1表示这种字符是奇数,否则是偶数。然后用一个数组记录每种状态f值最小的位置,然后对于转移,我们枚举其中一个字母的数量是奇是偶与当前状态不一样的一个(还有自身原来的),然后求一个f值的min,最后对找到的那个最小值+1就是这个位置的值了。

Code

#pragma GCC optimize(3)#include<bits/stdc++.h>using namespace std;typedef long long ll;bool Finish_read;template<class T>inline void read(T &x) {    Finish_read=0;x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;Finish_read=1;}template<class T>inline void print(T x) {    if(x/10!=0)        print(x/10);    putchar(x%10+'0');}template<class T>inline void writeln(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);    putchar('\n');}template<class T>inline void write(T x) {    if(x<0)        putchar('-');    x=abs(x);    print(x);}/*================Header Template==============*/const int mx=(1<<26);int pos[mx],f[200010];char s[200010];bitset<26> x[200010];int main() {    scanf("%s",s+1);    int n=strlen(s+1);    f[0]=0;    memset(pos,-1,sizeof pos);    pos[0]=0;    for(int i=1;i<=n;i++) {        x[i]^=x[i-1];        x[i][s[i]-'a']=1-x[i][s[i]-'a'];        f[i]=2e9;        int t=x[i].to_ulong(),ind=i;        for(int j=0;j<26;j++) {            int tmp=(1<<j)^t;            if(pos[tmp]!=-1&&f[ind]>f[pos[tmp]])                ind=pos[tmp];        }        if(pos[t]!=-1&&f[ind]>f[pos[t]])            ind=pos[t];        f[i]=f[ind]+1;        if(pos[t]==-1||f[i]<f[pos[t]])            pos[t]=i;    }    printf("%d\n",f[n]);    return 0;}