AtCoder Grand contests 040

A - Engines AtCoder 4900

Problem Statement
E869120 is initially standing at the origin (0,0) in a two-dimensional plane.
He has N engines, which can be used as follows:
When E869120 uses the i-th engine, his X- and Y-coordinate change by xi and yi, respectively. In other words, if E869120 uses the i-th engine from coordinates (X,Y), he will move to the coordinates (X+xi,Y+yi).
E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X,Y) be his final coordinates. Find the maximum possible value of √X2+Y2 , the distance from the origin.
Constraints
1≤N≤100
−1 000 000≤xi≤1 000 000
−1 000 000≤yi≤1 000 000
All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
x1 y1
x2 y2
: :
xN yN
Output
Print the maximum possible final distance from the origin, as a real value. Your output is considered correct when the relative or absolute error from the true answer is at most 10−10.
Sample Input 1
3
0 10
5 -5
-5 -5
Sample Output 1
10.000000000000000000000000000000000000000000000000
The final distance from the origin can be 10 if we use the engines in one of the following three ways:
Use Engine 1 to move to (0,10).
Use Engine 2 to move to (5,−5), and then use Engine 3 to move to (0,−10).
Use Engine 3 to move to (−5,−5), and then use Engine 2 to move to (0,−10).
The distance cannot be greater than 10, so the maximum possible distance is 10.
Sample Input 2
5
1 1
1 0
0 1
-1 0
0 -1
Sample Output 2
2.828427124746190097603377448419396157139343750753
The maximum possible final distance is 2√2 =2.82842…. One of the ways to achieve it is:
Use Engine 1 to move to (1,1), and then use Engine 2 to move to (2,1), and finally use Engine 3 to move to (2,2).
Sample Input 3
5
1 1
2 2
3 3
4 4
5 5
Sample Output 3
21.213203435596425732025330863145471178545078130654
If we use all the engines in the order 1→2→3→4→5, we will end up at (15,15), with the distance 15√2 =21.2132… from the origin.
Sample Input 4
3
0 0
0 1
1 0
Sample Output 4
1.414213562373095048801688724209698078569671875376
There can be useless engines with (xi,yi)=(0,0).
Sample Input 5
1
90447 91000
Sample Output 5
128303.000000000000000000000000000000000000000000000000
Note that there can be only one engine.
Sample Input 6
2
96000 -72000
-72000 54000
Sample Output 6
120000.000000000000000000000000000000000000000000000000
There can be only two engines, too.
Sample Input 7
10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
Sample Output 7
148.660687473185055226120082139313966514489855137208

题意:给出n个向量,然后把这n个向量相加,求相加之后距原点最远的距离是多少
先按照极角排序,然后再双重循环

#include
#include
#include
using namespace std;

struct node{
	int x;int y;
}aa[456];

bool cmp(node a,notiyi
{
	return atan2(a.x,a.y)<atan2(b.x,b.y);
}
int main()
{
	long long n,x,y;
	long long ans=0;
	cin>>n;
	for(int i=0;i<n;i++)	cin>>aa[i].x>>aa[i].y;
	sort(aa,aa+n,cmp);
	for(int i=0;i<n;i++)
	{
		x=aa[i].x;y=aa[i].y;
		ans=max(ans,x*x+y*y);
		for(int j=(i+1)%n;j!=i;j=(j+1)%n)
		{
			x+=aa[j].x;y+=aa[j].y;
			ans=max(ans,x*x+y*y);
		}
	}
	double res=sqrt(ans);
	printf("%.13lf\n",res);
	return 0;
}

B - Consecutive Integers Atcoder 5037

Problem Statement
Snuke has N integers: 1,2,[ldots],N. He will choose K of them and give those to Takahashi. How many ways are there to choose K consecutive integers?
Constraints
All values in input are integers.
1≤K≤N≤50
Input
Input is given from Standard Input in the following format:
N K
Output
Print the answer.
Sample Input 1
3 2
Sample Output 1
2
There are two ways to choose two consecutive integers: (1,2) and (2,3).
Sample Input 2
13 3
Sample Output 2
11

#include
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	cout<<n-m+1<<endl;
	return 0;
}

C - ModSum AtCoder 4873

Problem Statement
For an integer N, we will choose a permutation {P1,P2,…,PN} of {1,2,…,N}. Then, for each i=1,2,…,N, let Mi be the remainder when i is divided by Pi.
Find the maximum possible value of M1+M2+[cdots]+MN.
Constraints
N is an integer satisfying 1≤N≤109.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible value of M1+M2+[cdots]+MN.
Sample Input 1
2
Sample Output 1
1
When the permutation {P1,P2}={2,1} is chosen, M1+M2=1+0=1.
Sample Input 2
13
Sample Output 2
78
Sample Input 3
1
Sample Output 3
0

#include
using namespace std;
int main()
{
	long long n;
	cin>>n;
	long long res;
	res=n*(0+n-1)/2;
	cout<<res<<endl;
	return 0;
}

D - Shortest Path on a Line AtCoder 5635

Problem Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the vertices. In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph. The i-th operation is as follows:
The operation uses integers Li and Ri between 1 and N (inclusive), and a positive integer Ci. For every pair of integers (s,t) such that Li≤s The integers L1,…,LM, R1,…,RM, C1,…,CM are all given as input.
Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.
Constraints
2≤N≤105
1≤M≤105
1≤Li 1≤Ci≤109
Input
Input is given from Standard Input in the following format:
N M
L1 R1 C1
:
LM RM CM
Output
Print the length of the shortest path from Vertex 1 to Vertex N in the final graph. If there is no shortest path, print -1 instead.
Sample Input 1
4 3
1 3 2
2 4 3
1 4 6
Sample Output 1
5
We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.
Sample Input 2
4 2
1 2 1
3 4 2
Sample Output 2
-1
Sample Input 3
10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3
Sample Output 3
28

daibu

E - Counting of Trees AtCoder 5633

Problem Statement
Given is an integer sequence D1,…,DN of N elements. Find the number, modulo 998244353, of trees ith N vertices numbered 1 to N that satisfy the following condition:
For every integer i from 1 to N, the distance between Vertex 1 and Vertex i is Di.
Notes
A tree of N vertices is a connected undirected graph with N vertices and N−1 edges, and the distance between two vertices are the number of edges in the shortest path between them.
Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.
Constraints
1≤N≤105
0≤Di≤N−1
Input
Input is given from Standard Input in the following format:
N
D1 D2 … DN
Output
Print the answer.
Sample Input 1
4
0 1 1 2
Sample Output 1
2
For example, a tree with edges (1,2), (1,3), and (2,4) satisfies the condition.
Sample Input 2
4
1 1 1 1
Sample Output 2
0
Sample Input 3
: 7
0 3 2 1 2 2 1
Sample Output 3
24

题意;先输入一个数n,然后再输入n个数,比如说最后一个样例0 3 2 1 2 2 1
0是第一 棵树到第一棵树的距离,3是第二棵树到第一棵树的距离,2是第三棵树到第一棵树的距离,1是第四棵树到第一棵树的距离,也就是说第一个数字必须是0,如果不是0直接返回0,而且这些点必须连续,如果第三层有点但是第二层没有点就直接返回0

#include
using namespace std;tiyiypedef long long ll;
const ll mod=998244353;
ll aa[123456];

ll ksm(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res%mod;
}
int main()
{
	int n,x;int maxx=0;
	ll count=1;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		if(i==1&&x!=0) count=0;
		aa[x]++;
		maxx=max(x,maxx); 
	}	
	for(int i=maxx;i>1;i--)
	{
		if(aa[i]==0) count=0;
		count=count*ksm(aa[i-1],aa[i])%mod;
	}
	if(aa[0]>1) count=0;
	cout<<count<<endl;
	return 0;
 } 

F - Monsters Battle Royale AtCoder 4297

Problem Statement
There are N monsters, numbered 1,2,…,N.
Initially, the health of Monster i is Ai. Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
A random alive monster attacks another random alive monster.
As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.
Find the minimum possible final health of the last monster alive.
Constraints
All values in input are integers.
2≤N≤105
1≤Ai≤109
Input
Input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print the minimum possible final health of the last monster alive.
Sample Input 1
4
2 10 8 40
Sample Output 1
2
When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.
Sample Input 2
4
5 13 8 1000000000
Sample Output 2
1
Sample Input 3
3
1000000000 1000000000 1000000000
Sample Output 3
1000000000

这个题,,看到的时候就在蒙圈,知道题意但是不知道怎么写
然后不知道怎么就突然想到了最大公约数,然后试着交了一个,竟然AC了,,,,,

#include
#include
using namespace std;
int main()
{
	int n;long long a,b;
	cin>>n;
	cin>>a;
	for(int i=1;i<n;i++)
	{
		cin>>b;
		a=__gcd(a,b);
	}
	cout<<a<<endl;
	return 0;
}

G - Powerful Discount Tickets AtCoder 4864

Problem Statement
Takahashi is going to buy N items one by one.
The price of the i-th item he buys is Ai yen (the currency of Japan).
He has M discount tickets, and he can use any number of them when buying an item.
If Y tickets are used when buying an item priced X yen, he can get the item for X
2Y
(rounded down to the nearest integer) yen.
What is the minimum amount of money required to buy all the items?
Constraints
All values in input are integers.
1≤N,M≤105
1≤Ai≤109
Input
Input is given from Standard Input in the following format:
N M
A1 A2 … AN
Output
Print the minimum amount of money required to buy all the items.
Sample Input 1
3 3
2 13 8
Sample Output 1
9
We can buy all the items for 9 yen, as follows:
Buy the 1-st item for 2 yen without tickets.
Buy the 2-nd item for 3 yen with 2 tickets.
Buy the 3-rd item for 4 yen with 1 ticket.
Sample Input 2
4 4
1 9 3 5
Sample Output 2
6
Sample Input 3
1 100000
1000000000
Sample Output 3
0
We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.
Sample Input 4
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 4
9500000000

这个题也是知道了题意但是不知道怎么写,而且过的人还挺多的,,,
然后突然就想起来了优先队列

#include
#include
#include
using namespace std;
int main()
{
	long long n,m;int x;long long sum=0;
	cin>>n>>m;
	priority_queue<int,vector<int>,less<int> >qq;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		qq.push(x);
	}
	while(m--)
	{
		x=qq.top();
		qq.pop();
		qq.push(x/2);
	}
	while(qq.size())
	{
		sum=sum+qq.top();
		qq.pop();
	}
	cout<<sum<<endl;
	return 0;
}

H - Attack Survival AtCoder 4870

Problem Statement
Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players’ scores in a game, which proceeds as follows.
A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.
When a player correctly answers a question, each of the other N−1 players receives minus one (−1) point. There is no other factor that affects the players’ scores.
At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.
In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player Ai. For Kizahashi, write a program that determines whether each of the N players survived this game.
Constraints
All values in input are integers.
2≤N≤105
1≤K≤109
1≤Q≤105
1≤Ai≤N (1≤i≤Q)
Input
Input is given from Standard Input in the following format:
N K Q
A1
A2
.
.
.
AQ
Output
Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.
Sample Input 1
6 3 4
3
1
3
2
Sample Output 1
No
No
Yes
No
No
No
In the beginning, the players’ scores are (3,3,3,3,3,3).
Player 3 correctly answers a question. The players’ scores are now (2,2,3,2,2,2).
Player 1 correctly answers a question. The players’ scores are now (2,1,2,1,1,1).
Player 3 correctly answers a question. The players’ scores are now (1,0,2,0,0,0).
Player 2 correctly answers a question. The players’ scores are now (0,0,1,−1,−1,−1).
Players 1,2,4,5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.
Sample Input 2
6 5 4
3
1
3
2
Sample Output 2
Yes
Yes
Yes
Yes
Yes
Yes
Sample Input 3
10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
Sample Output 3
No
No
No
No
Yes
No
No
No
Yes
No

差分数组

#include
#include
#include
using namespace std;
int bb[123456];
void insert(int a,int b,int c)
{
	bb[a]+=c;
	bb[b+1]-=c;
}
int main()
{
	int n,m,q;int x;
	cin>>n>>m>>q;
	bb[0]=m;
	for(int i=0;i<q;i++)
	{
		cin>>x;
		insert(1,x-1,-1);
		insert(x+1,n+1,-1);
	}
	for(int i=1;i<=n;i++)
	{
		bb[i]+=bb[i-1];
		if(bb[i]>0) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}

I - Lower AtCoder 4871

Problem Statement
There are N squares arranged in a row from left to right.
The height of the i-th square from the left is Hi.
You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.
Find the maximum number of times you can move.
Constraints
All values in input are integers.
1≤N≤105
1≤Hi≤109
Input
Input is given from Standard Input in the following format:
N
H1 H2 … HN
Output
Print the maximum number of times you can move.
Sample Input 1
5
10 4 8 7 3
Sample Output 1
2
By landing on the third square from the left, you can move to the right twice.
Sample Input 2
7
4 4 5 6 6 5 5
Sample Output 2
3
By landing on the fourth square from the left, you can move to the right three times.
Sample Input 3
4
1 2 3 4
Sample Output 3
0

#include
using namespace std;
long long aa[123456];
int dp[123456];

int main()
{
	int n;int maxx=0;int count=0;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>aa[i];
	for(int i=1;i<=n;i++)
	{
		if(aa[i]<=aa[i-1]) 
		{
			dp[i]=dp[i-1]+1;
			maxx=max(maxx,dp[i]);
			count=i;
		}
	}
	cout<<maxx<<endl;
}

J - Kleene Inversion AtCoder 5165

Problem Statement
We have a sequence of N integers A=A0,A1,…,~AN−1.
Let B be a sequence of K×N integers obtained by concatenating K copies of A. For example, if A=1,3,2 and K=2, B=1,3,2,1,3,~2.
Find the inversion number of B, modulo 109+7.
Here the inversion number of B is defined as the number of ordered pairs of integers (i,j)(0≤iBj.
Constraints
All values in input are integers.
1≤N≤2000
1≤K≤109
1≤Ai≤2000
Input
Input is given from Standard Input in the following format:
N K
A0 A1 … AN−1
Output
Print the inversion number of B, modulo 109+7.
Sample Input 1
2 2
2 1
Sample Output 1
3
In this case, B=2,1,2,~1. We have:
B0>B1
B0>B3
B2>B3
Thus, the inversion number of B is 3.
Sample Input 2
3 5
1 1 1
Sample Output 2
0
A may contain multiple occurrences of the same number.
Sample Input 3
10 998244353
10 9 8 7 5 6 3 4 2 1
Sample Output 3
185297239
Be sure to print the output modulo 109+7.

感谢强哥改对了这代码,,
题意是说先输入两个数n和k
然后输入n个数字,把这n个数字重复k次,看每一个数后面有多少个数小于这个数,
可以先找到原来没有重复过的数组中每一个数的后面有多少个比他小的数,例如1,6,10,9,8,7这个例子中,10的后面有三个比他小的数,然后把这个数组重复3次,然后第一次中的10就有33个数比他小,然后·第二次中的10就有32个数比他小,第3次重复的数组中就有3*1个比他小的数,所以,这种情况下应该乘于(1+2+3+4+。。。。+n);
然后还是刚才的例子,1,6虽然在第一次中比10要小,但是在第二次重复中,第一次的10就要比第二次的1,6大了,而且第一次的数要在第二次的左边,是符合要求的,这种情况下,就应该乘于(1+2+3+.。。+n-1);

#include
using namespace std;

typedef long long ll;
const ll mod=1e9+7;
ll f[2345678];
ll c[2345678];
ll d[2345678];


ll ksm(ll a,ll b){
	ll res = 1;
	while(b){
		if(b&1) res = res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}
int main()
{
   int n;
   ll k,count=0;
   cin>>n>>k;
   for(int i=0;i<n;i++)  cin>>f[i];
   for(int i=0;i<n;i++)
   {
       for(int j=i+1;j<n;j++)
           if(f[i]>f[j]) c[i]++;//求这个数的右边有多少小于f[i]的
    
       for(int j=0;j<i;j++)
           if(f[i]>f[j]) d[i]++;//求这个数左边有多少小于f[i]的
    }
    for(int i=0;i<n;i++)
    {    	
		count=(count+c[i]*k%mod*(1+k)%mod*ksm(2,mod-2)%mod)%mod;
    	count=(count+d[i]*(k-1)%mod*k%mod*ksm(2,mod-2)%mod)%mod;
	}
    cout<<count<<endl;
}

这个是折磨我两天的代码,这么短的代码也没改对,,,,,

#include
using namespace std;

typedef long long ll;
const ll mod=1e9+7;
int f[2345];int c[2345];int d[2345];
int main()
{
   int n;ll k,count=0;
   cin>>n>>k;
   for(int i=1;i<=n;i++)  cin>>f[i];
   for(int i=1;i<=n;i++)
       for(int j=i+1;j<=n;j++)
           if(f[i]>f[j]) c[i]++;
    for(int i=1;i<=n;i++)
       for(int j=1;j<i;j++)
           if(f[i]>f[j]) d[i]++;
    for(int i=1;i<=n;i++)
    {
    	count=(count+c[i]*(k*(1+k)/2)%mod)%mod;
    	count=(count+(d[i]*(k-1)*k/2)%mod)%mod;
	}
    cout<<count<<endl;
}

K - Two Contests AtCoder 5660

Problem Statement

109 contestants, numbered 1 to 109, will compete in a competition. There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests. When Problem i is presented in a contest, it will be solved by all contestants from Contestant Li to Contestant Ri (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests. Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest. Find the maximum possible total joyfulness of the two contests.

Constraints

    2≤N≤105
    1≤Li≤Ri≤109
    All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L1 R1
L2 R2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

    Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.
    Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.
    The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931

待补

L - Get Everything AtCoder 5217

Problem Statement
We have N locked treasure boxes, numbered 1 to N.
A shop sells M keys. The i-th key is sold for ai yen (the currency of Japan), and it can unlock bi of the boxes: Box ci1, ci2, …, cibi. Each key purchased can be used any number of times.
Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print −1.
Constraints
All values in input are integers.
1≤N≤12
1≤M≤103
1≤ai≤105
1≤bi≤N
1≤ci1 Input
Input is given from Standard Input in the following format:
N M
a1 b1
c11 c12 … c1b1
:
aM bM
cM1 cM2 … cMbM
Output
Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print −1.
Sample Input 1
2 3
10 1
1
15 1
2
30 2
1 2
Sample Output 1
25
We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.
Sample Input 2
12 1
100000 1
2
Sample Output 2
-1
We cannot unlock all the boxes.
Sample Input 3
4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3
Sample Output 3
69942

M - AB Substrings AtCoder 5039

Problem Statement

Snuke has N strings. The i-th string is si.

Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

    1≤N≤104
    2≤|si|≤10
    si consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4
#include
#include
using namespace std;
int main()
{
	int n;int a=0,b=0,ab=0;int count=0;
	string ss;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>ss;
		int len=ss.length();
		if(ss[0]=='B') b++;
		if(ss[len-1]=='A') a++;
		if(ss[0]=='B'&&ss[len-1]=='A') ab++;
		for(int j=1;j<len;j++)
		{
			if(ss[j]=='B'&&ss[j-1]=='A')count++;
		}
	}
	if(a==b&&b==ab&&b!=0) count--;
	cout<<count+min(a,b)<<endl;
	return 0;
}

附上错误代码
又是一个很久都没改出来的

#include
#include
using namespace std;
int main()
{
	int n;int a=0,b=0;int count=0;
	string ss;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>ss;
		int len=ss.length();
		if(ss[0]=='B') b++;
		if(ss[len-1]=='A') a++;
		for(int j=1;j<len;j++)
		{
			if(ss[j]=='B'&&ss[j-1]=='A')count++;
		}
	}
	cout<<count+min(a,b)<<endl;
	return 0;
}

你可能感兴趣的:(AtCoder Grand contests 040)