今日头条两条答案

//手串
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
public class p1 {
	public static void main(String[] args) {
		Scanner sc  =new Scanner(System.in);
		while(sc.hasNext())
		{
			int num =sc.nextInt();
			int m =sc.nextInt();
			int c =sc.nextInt();
			HashMap <Integer,TreeSet<Integer>> hashmap =new HashMap<Integer,TreeSet<Integer>>();
			int  sequence=0;
			int ans =0;
			
			for(int i=0;i<num;i++)
			{   
				int cN=sc.nextInt();
				for(int j=0;j<cN;j++)
				{
					int color =sc.nextInt();
					if(hashmap.containsKey(color))
					{  
						TreeSet <Integer> l =new TreeSet<Integer>(); 
						l =hashmap.get(color);
						l.add(sequence);
						hashmap.put(color, l);
					}
					else
					{
						TreeSet <Integer> l =new TreeSet<Integer>(); 
						l.add(sequence);
						hashmap.put(color, l);	
					}
				}
				sequence++;
				//无色珠子我就不管了	
			}
			for(int i=1;i<=c;i++)
			{   TreeSet <Integer> l =new TreeSet<Integer>(); 
				l =hashmap.get(i);
				if(l!=null)
				{
				int count =0;
				int temp =0;
				int temp2 =0;//记录中间变量
				int total =l.size();
				int flag=0;
			    for(Integer j:l)
			    {   
			    	if(count ==0)
			    	{
			    		temp =j;
			    		//记录下第一个变量
			    	}
			    	if(count ==total-1)
			    	{
			    		if(temp+num-j<m)
			    		{
			    			flag=1;
			    		}
			    	}
			    	if(count!=0)
			    	{
			    		if(j-temp2<m)
			    		{
			    			flag=1;
			    		}
			    	}
			    	
			    	temp2 =j;
			    	count++;
			    }
			    if(flag==1)
			    {
			    	ans++;
			    }
				}
			}
			System.out.println(ans);	
		}	
	}
}
//爱好
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
public class p2 {
	public static void main(String[] args) {
		Scanner sc =new Scanner (System.in);
		while(sc.hasNext())
		{   
			int num1 =sc.nextInt();
			HashMap <Integer,TreeSet<Integer>> hashmap1 =new HashMap<Integer,TreeSet<Integer>>();
			
			for(int i =1;i<=num1;i++)
			{   int hobby1 =sc.nextInt();
				
				if(hashmap1.containsKey(hobby1))
				{  
					TreeSet <Integer> l1 =new TreeSet<Integer>(); 
					l1 =hashmap1.get(hobby1);
					l1.add(i);
					hashmap1.put(hobby1, l1);
				}
				else
				{
					TreeSet <Integer> l =new TreeSet<Integer>(); 
					l.add(i);
					hashmap1.put(hobby1, l);	
				}
			}
			int q =sc.nextInt();
			int  [] ans =new int [q];
			for(int i=0;i<q;i++)
			{   int  first =sc.nextInt();
			    int  end =sc.nextInt();
			    int h =sc.nextInt();
			    TreeSet <Integer> l=hashmap1.get(h);
			    int count =0;
			    if(l!=null)
			    {
			    	for(Integer j:l)
			    	{
			    		if(j>=first&&j<=end)
			    		{
			    			count++;
			    		}
			    	} 	
			    }
			    	ans[i] =count;
			}
			for(int i=0;i<q;i++)
			{
				System.out.println(ans[i]);
			}
		}
	}
}
//


全部评论
膜拜大神。。。
点赞 回复 分享
发布于 2017-09-10 21:06
import java.util.ArrayList; import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); //珠子的个数 int m=sc.nextInt(); //连续不能出现同一种颜色的珠子数 int c=sc.nextInt(); //颜色的种类 ArrayList<ArrayList<Integer>> arr=new ArrayList<ArrayList<Integer>>(); for(int i=0;i<n;i++){ ArrayList<Integer> list=new ArrayList<Integer>(); int k=sc.nextInt(); for(int j=0;j<k;j++){ list.add(sc.nextInt()); } arr.add(list); } f(arr,n,m,c); } } private static void f(ArrayList<ArrayList<Integer>> arr, int n, int m, int c) { if(m==1){ System.out.println(0); return; } ArrayList<ArrayList<Integer>> p=new ArrayList<ArrayList<Integer>>(); int sum=0; for(int i=1;i<=c;i++){   //每一种颜色进行判断      ArrayList<Integer> list=new ArrayList<Integer>();      int count=1;      for(ArrayList<Integer> t:arr){      for(int j=0;j<t.size();j++){      if(t.get(j)==i){      list.add(count);      t.remove(Integer.valueOf(i));      }      }      count++;      }      p.add(list); } for(ArrayList<Integer> t:p){ if(t.size()==1){ continue; } if((t.get(0)==1)&&(t.get(t.size()-1)==n)){ sum++; continue; } for(int i=1;i<t.size();i++){ if((t.get(i)-t.get(i-1))<m){ sum++; continue; } } } System.out.println(sum); } }
点赞 回复 分享
发布于 2017-09-10 22:25

相关推荐

litbisc:你先说会,然后去速成课自学一个月,出门在外,身份都是自己给的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务