Colored Stone Necklace

Topic description

There is a colored gemstone necklace, which is composed of many different gemstones, including rubies, sapphires, diamonds, emeralds, pearls, etc. One day the king gave a necklace to a scholar and said to him, you can take this necklace with you, but the queen likes ruby, sapphire, amethyst, emerald and diamond very much, I want you to cut the chain from the necklace Give me back a small section, which must contain all of these five gems, and you can take the rest with you. If you can't find one, you can't take one. Please help the scholar figure out how to slice the necklace to get the most gems.

Enter description:

We use each character to represent a gem, A for ruby, B for sapphire, C for amethyst, D for emerald, E for diamond, F for jade, G for glass, etc. We use a character that is all capital letters Sequence represents the sequence of gemstones of the necklace, note that the necklaces are connected end to end. Each row represents a situation.

Output description:

Export the maximum number of gems a scholar can get. one per line
Example 1

enter

ABCYDYE
ATTMBQECPD

output

1
3
1  import java.util.Scanner;
 2  /* 
3  * 
 4  * take gems
 5  * 1, so take
 6  * 2 under the double string, take the length as the loop condition 5<length<list.leng
 7  * 3, Traverse and take out a string of length length to determine if it contains contain()
 8   */ 
9  public  class Main {
 10  static  public  int f(String str) {
 11      String ss = str+ str;
 12      for ( int i = 5; i <=str .length(); i++ ) {
 13          for ( int j = 0; j <=str.length(); j++ ) {
14             String s = ss.substring(j,j+i);
15             if (s.contains("A")&&s.contains("B")
16         &&s.contains("C")&&s.contains("D")&&s.contains("E")) {
17                 return  str.length()-i;
18             }
19         }
20     }
21     return 0;
22 }
23 public static void main(String[] args) {
24     Scanner sc = new Scanner(System.in);
25     String str = sc.nextLine();
26     String ss = str+str;
27     int res = f(str);
28     System.out.println(res);
29 }
30 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325165712&siteId=291194637