牛客[编程题] HJ25 数据分类处理
描述
信息社会,有海量的数据需要分析处理,比如公安局分析身份证号码、 QQ 用户、手机号码、银行帐号等信息及活动记录。
输入描述:
一组输入整数序列I和一组规则整数序列R,I和R序列的第一个整数为序列的个数(个数不包含第一个整数);整数范围为0~(2^31)-1,序列个数不限
输出描述:
从R依次中取出R<i>,对I进行处理,找到满足条件的I:
I整数对应的数字需要连续包含R<i>对应的数字。比如R<i>为23,I为231,那么I包含了R<i>,条件满足 。
按R<i>从小到大的顺序:
(1)先输出R<i>;
(2)再输出满足条件的I的个数;
(3)然后输出满足条件的I在I序列中的位置索引(从0开始);
(4)最后再输出I。
附加条件:
(1)R<i>需要从小到大排序。相同的R<i>只需要输出索引小的以及满足条件的I,索引大的需要过滤掉
(2)如果没有满足条件的I,对应的R<i>不用输出
(3)最后需要在输出序列的第一个整数位置记录后续整数序列的个数(不包含“个数”本身)
序列I:15,123,456,786,453,46,7,5,3,665,453456,745,456,786,453,123(第一个15表明后续有15个整数)
序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)
输出:30, 3,6,0,123,3,453,7,3,9,453456,13,453,14,123,6,7,1,456,2,786,4,46,8,665,9,453456,11,456,12,786
说明:
30----后续有30个整数
3----从小到大排序,第一个R<i>为0,但没有满足条件的I,不输出0,而下一个R<i>是3
6--- 存在6个包含3的I
0--- 123所在的原序号为0
123--- 123包含3,满足条件
示例1
15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123 5 6 3 6 3 0
30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786
将序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)排序去重后,可得0,3,6。 序列I没有包含0的元素。 序列I中包含3的元素有:I[0]的值为123、I[3]的值为453、I[7]的值为3、I[9]的值为453456、I[13]的值为453、I[14]的值为123。 序列I中包含6的元素有:I[1]的值为456、I[2]的值为786、I[4]的值为46、I[8]的值为665、I[9]的值为453456、I[11]的值为456、I[12]的值为786。 最后按题目要求的格式进行输出即可。
using System.Collections.Generic; using System; public class Program { public static void Main() { string line; string line1 = null; string line2 = null; while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case if (line1 == null)line1 = line; else line2 = line; if (line1 == null || line2 == null)continue; var numbers1 = GetNumbers(line1, true); var numbers2 = GetNumbers(line2, false); if (numbers1.Count < 2 || numbers2.Count < 2)continue; numbers2.Sort(); List<int> res = new List<int>(); List<int> temp = new List<int>(); for (int i = 0; i < numbers2.Count; i++) { temp = new List<int>(); temp.Add(numbers2[i]); for (int j = 0; j < numbers1.Count; j++) { if (numbers1[j].ToString().Contains(numbers2[i].ToString())) { temp.Add(j ); temp.Add(numbers1[j]); } } if (temp.Count > 1&&temp.Count%2==1) { temp.Insert(1,(temp.Count-1)/2); res.AddRange(temp); } } string output = string.Empty; output += res.Count; foreach (var n in res) { output += " " + n.ToString(); } Console.WriteLine(output); } } public static List<int> GetNumbers(string line, bool allowRepeat) { List<int> numbers = new List<int>(); var arr = line.Split(' '); int number; for (int i=1;i<arr.Length;i++) { if (int.TryParse(arr[i], out number)) { if (!allowRepeat && numbers.Contains(number))continue; numbers.Add(number); } } return numbers; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律