You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
ABA
NO
BACFAB
YES
AXBYBXA
NO
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings: BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
题意:判断字符串中是否有两个不重叠的“AB”"BA"有则输出“YES” 否则输出“NO”
题解:模拟判断,标记。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll __int64 4 int n; 5 char a[100005]; 6 map<int,int>mp1; 7 map<int,int>mp2; 8 int main () 9 { 10 scanf("%s",a); 11 int len=strlen(a); 12 int flag1=0,flag2=0,flag3=0,flag4=0; 13 for(int i=0;i<len-1;i++) 14 { 15 if(a[i]=='A'&&a[i+1]=='B') 16 { 17 mp1[i+1]=1; 18 mp2[i]=1; 19 flag1=1; 20 break; 21 } 22 } 23 for(int i=0;i<len-1;i++) 24 { 25 if(mp1[i]==0&&mp2[i+1]==0&&a[i]=='B'&&a[i+1]=='A') 26 { 27 flag2=1; 28 break; 29 } 30 } 31 mp1.clear(); 32 mp2.clear(); 33 for(int i=0;i<len-1;i++) 34 { 35 if(a[i]=='B'&&a[i+1]=='A') 36 { 37 mp1[i+1]=1; 38 mp2[i]=1; 39 flag3=1; 40 break; 41 } 42 } 43 for(int i=0;i<len-1;i++) 44 { 45 if(mp1[i]==0&&mp2[i+1]==0&&a[i]=='A'&&a[i+1]=='B') 46 { 47 flag4=1; 48 break; 49 } 50 } 51 if((flag1==1&&flag2==1)||(flag3==1&&flag4==1)) 52 printf("YES "); 53 else 54 printf("NO "); 55 return 0; 56 }
You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.
Find the number of ways to choose a problemset for the contest.
The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 106) — the difficulty of each problem.
Print the number of ways to choose a suitable problemset for the contest.
3 5 6 1
1 2 3
2
4 40 50 10
10 20 30 25
2
5 25 35 10
10 10 20 10 20
6
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
题意:给你n个数,选取若干个数,使得数的和在[l,r]的范围内 并且最大值与最小值的差值大于等于x 问有多少种选择的方案
题解:n为15 共有(2^15)中选择方案 枚举check。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<set> 7 #include<vector> 8 using namespace std; 9 int n,l,r,x; 10 int a[20]; 11 int main() 12 { 13 scanf("%d %d %d %d",&n,&l,&r,&x); 14 for(int i=0;i<n;i++) 15 scanf("%d",&a[i]); 16 int cnt=1<<n; 17 int ans=0; 18 for(int i=1;i<cnt;i++) 19 { 20 int minx=1e9+1,maxn=-1; 21 int exm=i; 22 int sum=0; 23 int jishu=0; 24 while(exm>0) 25 { 26 //cout<<exm<<endl; 27 if(exm%2==1) 28 { 29 minx=min(minx,a[jishu]); 30 maxn=max(maxn,a[jishu]); 31 sum+=a[jishu]; 32 } 33 exm=exm/2; 34 jishu++; 35 } 36 37 if((maxn-minx)>=x&&sum>=l&&sum<=r) 38 ans++; 39 } 40 printf("%d ",ans); 41 return 0; 42 }
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
3454
YES
344
10
YES
0
111111
NO
题意:给你一个数 问是否能够 通过删除若干个数,并且剩下的数的相对位置不变 组成的数能够除尽8,若能则输出这个组成的数
题解:1000可以除尽8 所以只需要暴力考虑一位 两位 三位的组成.
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<set> 7 #include<vector> 8 using namespace std; 9 char a[105]; 10 int main() 11 { 12 scanf("%s",a); 13 int len=strlen(a); 14 for(int i=0;i<len;++i) 15 { 16 if(a[i]=='0'||a[i]=='8'){ 17 printf("YES %c ",a[i]); 18 return 0; 19 } 20 } 21 for(int i=0;i<len-1;++i) 22 { 23 for(int k=i+1;k<len;++k){ 24 if(((a[i]-'0')*10+(a[k]-'0'))%8==0) 25 { 26 printf("YES %c%c ",a[i],a[k]); 27 return 0; 28 } 29 } 30 } 31 for(int i=0;i<len-2;++i) 32 { 33 for(int k=i+1;k<len-1;++k) 34 { 35 for(int l=k+1;l<len;++l) 36 { 37 if(((a[i]-'0')*100+(a[k]-'0')*10+(a[l]-'0'))%8==0) 38 { 39 printf("YES %c%c%c ",a[i],a[k],a[l]); 40 return 0; 41 } 42 } 43 } 44 } 45 printf("NO "); 46 return 0; 47 }
An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.
Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.
The single line of the input contains integer k (1 ≤ k ≤ 100) — the required degree of the vertices of the regular graph.
Print "NO" (without quotes), if such graph doesn't exist.
Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.
The description of the made graph must start with numbers n and m — the number of vertices and edges respectively.
Each of the next m lines must contain two integers, a and b (1 ≤ a, b ≤ n, a ≠ b), that mean that there is an edge connecting the vertices a and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal k. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order.
The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges).
1
YES
2 1
1 2
In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.
题意:构造一个 每个点的度都为k,并且最少有一条割边的图
题解:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<set> 7 #include<vector> 8 using namespace std; 9 int k; 10 int main() 11 { 12 scanf("%d",&k); 13 if(k==1){ 14 printf("YES "); 15 printf("2 1 1 2 "); 16 return 0; 17 } 18 if(k%2==0){ 19 printf("NO "); 20 return 0; 21 } 22 int n=k+2; 23 printf("YES %d %d ",n*2,n*k); 24 for(int i=1;i<=n;i++) 25 { 26 for(int j=i+1;j<=n;j++) 27 { 28 if(i==1&&j==n) continue; 29 if(j==i+1&&(i%2==1)) continue; 30 printf("%d %d ",i,j); 31 printf("%d %d ",n+i,n+j); 32 } 33 } 34 printf("%d %d ",1,n+1); 35 return 0; 36 }