BaoBao is taking a walk in the interval [0,n] on the number axis, but he is not free to move, as at every point (i−0.5) for all i∈[1,n], where i is an integer, stands a traffic light of type t
i
(t
i
∈{0,1}).
BaoBao decides to begin his walk from point p and end his walk at point q (both p and q are integers, and p
/*题目大意:Bao在数轴的间隔[0,n]上散步,但他不能自由移动,[1,n]处有n个路灯,
1代表绿灯可以行走,0代表红灯不能行走,红绿灯每过1秒都会改变(1变0,0变1),
求出走所有路径所用的时间(只能从前往后走,可以从0开始走,但路灯从1处才开始有)
解题思路:
例: 1 1 0 1 0
从0:1 3 4 5 6
从1: 1 2 3 4 除第一个数其他都为上一行数减二
从2: 2 3 4 除第一个数其他都和上一行数一样
从3: 1 2 所有数其他都为上一行数减二
从4: 2 除第一个数其他都都和上一行数一样
从中可以找出规律,若与前面的灯一样,则需要经过两秒,若不一样则只需一秒,
因为经过前面一定是绿灯,那此处本来是红灯,经过一秒刚好变成绿灯
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[100005];
char s[100005];
int main()
{
long long sum;//一定要用long long
int n, i, j, t;
scanf("%d", &t);
while(t--)
{
scanf("%s", &s);//先以字符串形式输入
n = strlen(s);
long long ans = 0, maxn = 0;
for(i = 0; i < n; i++)
{
a[i] = s[i] - '0';//变为数字形式
if(i == 0)
{
if(a[i] == 0)//如果开头为0,则需要等一秒再走,经过他共需两秒
maxn = 2;
else//如果开头为1,则直接走,只需一秒
maxn = 1;
}
else
{
if(a[i] == a[i-1])
maxn += 2;
else
maxn += 1;
//maxn表示从0点出发到达i点的时间
}
ans += maxn;//算出从0出发走完全过程的时间
}
sum = ans;//sum记录所有路程总时间
int m = n;
//下面是根据我写的例子所找出的规律
for(i = 1; i < n; i++)
{
if(a[i-1] == 1 && a[i] == 1)
{
ans = ans - ((m-1) * 2 + 1);
}
else if(a[i-1] == 1 && a[i] == 0)
{
ans = ans - 1;
}
else if(a[i-1] == 0 && a[i] == 1)
{
ans = ans - (m * 2);
}
else if(a[i-1] == 0 && a[i] == 0)
{
ans = ans - (m * 2);
}
sum += ans;
m--;
}
printf("%lld\n", sum);
}
return 0;
}