fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. string data = @"aaa bb 10 cc 11 dd 12 ee 13
  10. aaa cc 9 dd 4 bb 2 ee 13
  11. aaa cc 16 bb 9 dd 8 ee 13
  12. bbb a1 6 a2 9 a3 8
  13. bbb a2 7 a3 4 a1 6";
  14. string result = string.Join("\r\n", data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)
  15. .Select(x => x.Split(' ')).SelectMany(x => Enumerable.Range(0, x.Count() / 2).Select(y => new { a = x[0], b = x[y * 2 + 1], c = int.Parse(x[y * 2 + 2]) }))
  16. .GroupBy(x => x.a)
  17. .Select(x => new { a = x.Key + " " + x.Count() + "次", b = x.GroupBy(y => y.b).Select(y => new { b = y.Key, c = y.Sum(z => z.c) }) })
  18. .Select(x => x.a + " " + string.Join(" ", x.b.Select(y => y.b + " " + y.c.ToString()))));
  19. Console.WriteLine(result);
  20. }
  21. }
Success #stdin #stdout 0.07s 24224KB
stdin
Standard input is empty
stdout
aaa 12次 bb 21 cc 36 dd 24 ee 39
bbb 6次 a1 12 a2 16 a3 12