HDU-5198-Strange Class(Java+注意细节!)

Strange Class

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 634    Accepted Submission(s): 343


Problem Description
In Vivid’s school, there is a strange class(SC). In SC, the students’ names are very strange. They are in the same format:  anbncn (a,b,c must not be the same with each other). For example studens whose names are“abc”,”ddppqq” are in SC, however studens whose names are “aaa”,“ab”,”ddppqqq” are not in SC.
Vivid makes friends with so many students, he wants to know who are in SC.
 

Input
There are multiple test cases (about 10), each case will give a string S which is the name of Vivid’s friend in a single line.
Please process to the end of file.

[Technical Specification]

1|S|10 .

|S| indicates the length of S.

S only contains lowercase letter.
 

Output
For each case, output YES if Vivid’s friend is the student of SC, otherwise output NO.
 

Sample Input
   
   
   
   
abc bc
 

Sample Output
   
   
   
   
YES NO
 

Source
BestCoder Round #36 ($)
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5213  5212  5211  5209  5208 


英文不好的童鞋,看最后,这是一次BestCoder上的水题.....

这题很简单!但是要注意细节!
官方提示:(简单明了!)
1001 Strange Class
这是一个简单题,先判断长度是不是3的倍数。
然后再判断前中后三段里面的字符是不是一样。
最后判断一下这三段之间的字符是不一样的。



import java.io.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		while (input.hasNext())
		{
			boolean flag = true;
			String str = input.nextLine();
			char c[] = str.toCharArray();
   
			if (c.length % 3 != 0)                                      //不是3的倍数直接false
			{
				flag = false;
			}

			for (int i = 1; i < (c.length / 3); i++)                     //前1/3不相等直接false
			{
				if (c[i] != c[i - 1])
				{
					flag = false;
				}
			}
			for (int i = c.length / 3 + 1; i < (c.length * 2 / 3); i++)   //1/3到2/3的部分不相等直接false
			{
				if (c[i] != c[i - 1])
				{
					flag = false;
				}
			}

			for (int i = c.length * 2 / 3 + 1; i < c.length; i++)        //2/3到最后的部分不相等直接false
			{
				if (c[i] != c[i - 1])
				{
					flag = false;
				}
			}

			if (!(c[0] != c[c.length / 3] && c[0] != c[c.length - 1] && c[c.length / 3] != c[c.length - 1]))
			{
				flag = false;                                       //分成三部分的数要互不相同!
			}

			if (flag)
			{
				System.out.println("YES");
			} else
			{
				System.out.println("NO");
			}

		}
	}

}


 



中文解释如下:

Strange Class

 
 Accepts: 519
 
 Submissions: 1749
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
在Vivid的学校里,有一个奇怪的班级(SC).在SC里,这些学生的名字非常奇怪。他们的名字形式是这样的
     
      anbncn
     (a,b,c两两不相同。).例如,叫”abc”,”ddppqq”的学生是在SC里的,然而叫”aaa”,”ab”,”ddppqqq”的同学并不是在SC里的。
Vivid交了许多的朋友,他想知道他们之中哪些人是在SC里的。
输入描述
多组测试数据(大概
     
      10
     组),每一个数据在一行中给出一个字符串S,代表Vivid一个朋友的名字。
请处理到文件末尾。

[参数约定]

     
      1|S|10.
     
|S| 是指S的长度.
S 只包含小写字母.
输出描述
对于每一个数据,如果Vivid的朋友是SC里的,那么输出YES,否则输出NO。
输入样例
abc
bc
输出样例
YES
NO


你可能感兴趣的:(java,ACM,HDU,BestCoder,注意细节)