1379A. Acacius and String + 1379B.B. Dubious Cyrpto


1379A
传送门

Acacius is studying strings theory. Today he came with the following
problem.

You are given a string s of length n consisting of lowercase English
letters and question marks. It is possible to replace question marks
with lowercase English letters in such a way that a string “abacaba”
occurs as a substring in a resulting string exactly once?

Each question mark should be replaced with exactly one lowercase
English letter. For example, string “a?b?c” can be transformed into
strings “aabbc” and “azbzc”, but can’t be transformed into strings
“aabc”, “a?bbc” and “babbc”.

Occurrence of a string t of length m in the string s of length n as a
substring is a index i (1≤i≤n−m+1) such that string s[i…i+m−1]
consisting of m consecutive symbols of s starting from i-th equals to
string t. For example string “ababa” has two occurrences of a string
“aba” as a substring with i=1 and i=3, but there are no occurrences of
a string “aba” in the string “acba” as a substring.

Please help Acacius to check if it is possible to replace all question
marks with lowercase English letters in such a way that a string
“abacaba” occurs as a substring in a resulting string exactly once.

Input First line of input contains an integer T (1≤T≤5000), number of
test cases. T pairs of lines with test case descriptions follow.

The first line of a test case description contains a single integer n
(7≤n≤50), length of a string s.

The second line of a test case description contains string s of length
n consisting of lowercase English letters and question marks.

Output For each test case output an answer for it.

In case if there is no way to replace question marks in string s with
a lowercase English letters in such a way that there is exactly one
occurrence of a string “abacaba” in the resulting string as a
substring output “No”.

Otherwise output “Yes” and in the next line output a resulting string
consisting of n lowercase English letters. If there are multiple
possible strings, output any.

You may print every letter in “Yes” and “No” in any case you want (so,
for example, the strings yEs, yes, Yes, and YES will all be recognized
as positive answer).

大概是比较简单的字符串处理?然而太饿了,所以没做(其实是练少了)
这应该是近来最难的div2A吧?

#include //1379A

using namespace std;
string s1,s2,s3;
int over_roll(string sc)//统计
{
    int cnt=0;
    for(int i=0;i<=sc.size()-s3.size();i++){
        if(sc.substr(i,s3.size())==s3)cnt++;
    }
    return cnt;
}
int main()
{
   ios::sync_with_stdio(0);
   int t;
   int n;
   bool output_check,circulate_check;

   s3="abacaba";
   cin>>t;
   while(t--){
    cin>>n;
    cin>>s1;
    output_check=0;
    for(int i=0;i<=n-s3.size();i++){
        s2=s1;
        circulate_check=0;
        for(int j=0;j<s3.size();j++){
            if(s2[j+i]!=s3[j]&&s2[j+i]!='?'){
                circulate_check=1;
                break;
            }
            s2[i+j]=s3[j];
        }
        if(!circulate_check&&over_roll(s2)==1){
            for(int j=0;j<n;j++){
                if(s2[j]=='?')s2[j]='y';
            }
            output_check=1;
            cout<<"Yes"<<endl;
            cout<<s2<<endl;
            break;
        }
    }
    if(!output_check)cout<<"No"<<endl;
   }
}


1379B
传送门

Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l≤a,b,c≤r, and then he computes the encrypted value m=n⋅a+b−c.
Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that
a, b and c are integers,
l≤a,b,c≤r,
there exists a strictly positive integer n, such that n⋅a+b−c=m.
Input
The first line contains the only integer t (1≤t≤20) — the number of test cases. The following t lines describe one test case each.
Each test case consists of three integers l, r and m (1≤l≤r≤500000, 1≤m≤1010). The numbers are such that the answer to the problem exists.
Output
For each test case output three integers a, b and c such that, l≤a,b,c≤r and there exists a strictly positive integer n such that n⋅a+b−c=m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions.

做B题时饿晕了,醒来比赛结束了
m+c-b 的取值区间是[m-r+l,m+r-l]
n=(m+c-b)/a 让n向下取值
遍历寻找一个a,让n*a的值在取值区间中
这时输出合法的b,c值就解出来了

#include 

using namespace std;

int main()
{
   ios::sync_with_stdio(0);
   long long t,l,r,m,a,up1,down1,ans;
   cin>>t;
   while(t--){
        cin>>l>>r>>m;
        long long tes;
        down1=m-r+l;//下界
        up1=m+r-l;//上界
        for(a=l;a<=r;a++){
            tes=(up1)/a;
            if(tes*a<=up1&&tes*a>=down1)break;//寻找一个可
                                             //以在取值区间中
                                             //发挥作用的a值
        }
        ans=r-m+tes*a;
        if(ans>r){//防止越界
            ans=l-m+tes*a;
            r=l;
        }
        cout<<a<<" "<<r<<" "<<ans<<endl;
   }
}

你可能感兴趣的:(codeforces)