Problem C: The Same Color
Time Limit: 1 Sec
Memory Limit: 128 MB
Submit: 0
Solved: 0
Description
Diao Yang has many balls with different colors. Today he wants to divide his balls into two groups. But he does not know how to divide. Then Diao Ze gives him a suggestion: first you choose a ball and put it into the first group. Then from the second ball, if the color of the ball is same to the last ball you has chosen, then you put the ball into the other group. Otherwise you put the ball into the same group with the last ball. Diao Yang thinks it is OK.
Then the problem is, Diao Yang wants to know the product of the number of two groups’ balls. Can you help him?
Input
The first line contains an integer T, indicating the number of test cases.
In each test case, there are several lines, each line contains only one string with lowercas, indicating the color of the ball Diao Yang has chosen currently. Diao Yang will stop choosing when the string is equal to “END”. Note that the string “END” does not mean a color.
You can assume that there are at most 100 lines in each test case and each string has at most 10 letters.
Output
For each test case, output the answer in a line.
Sample Input
3
yellow
yellow
pink
red
red
green
END
blue
black
purple
purple
END
rose
orange
brown
white
END
Sample Output
9
3
0
HINT
In the first test case, the color of the first group’s balls are yellow, red, green, 3 balls in total. The color of the second group’s balls are yellow, pink, red, 3 balls in total too. So the product is 3×3=9.
In the second test case, the answer is 3×1=3 and in the third test case the answer is 4×0=0.
解题思路:水模拟题,对于每个字符串,与前一个字符串比较,如果相同,放入另一组;如果不相同,放入同一组,然后输出两组字符串数的乘积
为了方便字符串比较,本人直接用了string类型,因为string可以直接利用"!="判不等,虽然cin输入会比scanf慢一点,但不影响大局
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100005;
const int M = 40;
const int inf = 100000000;
const int mod = 2009;
int ans[2];
int main()
{
int t,p;
string s,a;
scanf("%d",&t);
while(t--)
{
memset(ans,0,sizeof(ans));
p=0;a="";
while(cin>>s)
{
if(s=="END")
break;
if(s!=a)
ans[p]++;
else
{
p^=1;
ans[p]++;
}
a=s;
}
printf("%d\n",ans[0]*ans[1]);
}
return 0;
}
/**************************************************************
Problem: 14
Language: C++
Result: Accepted
Time:733 ms
Memory:1504 kb
****************************************************************/
菜鸟成长记