比较字符串和版本化它们 [英] Comparing strings and versioning them

查看:129
本文介绍了比较字符串和版本化它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要编写一个代码,比较每个字符串的字符串和数字,具体取决于它们是否相同。给定的字符串数可以不同,因此它必须是通用的。这是一个例子:



Hello all,

I need to write a code which is compares given the strings and numbers each string depending on if they are same or not. The given strings count can differ so it has to be generic. Here is an example:

Example 1:
Given strings: 
-XXX
-YYY
-ZZZ
-XXX

So the output should be
-1
-2
-3
-1
Example 2:
Given strings: 

-YYY
-XXX
-YYY
-ZZZ
-XXX
-ZZZ

So the output should be
-1
-2
-1
-3
-2
-3





所以我到目前为止我能找到有多少不同的字符串,但我没有做版本控制。



So what I do so far I can find the how many different strings there are but I fail to do the versioning.

var count = new Dictionary<string, int>();
foreach (string s in arrStrings)
{
    if (count.ContainsKey(s.ToString()))
    {
        count[s.ToString()]++;
    }
    else
    {
        count.Add(s.ToString(), 1);
    }
}
foreach (var item in count)
{
    Console.WriteLine("{0} : {1}", item.Key, item.Value);
}





有人可以告诉我吗?在此先感谢。



Can someone advise me? Thanks in advance.

推荐答案

保持一个字符串作为键的字典,它的索引作为值,但只有在键不在字典中时才设置值。



这样的东西可能适合你;



Keep a dictionary with the string as key, and it's index as value, but only set the value if the key is not already in the dictionary.

Something like this might work for you;

using System;
using System.Collections.Generic;
using System.Linq;

namespace Test {
  public class Program {
    public static void Main() {
      var strings = new[] {"YYY", "XXX", "YYY", "ZZZ", "XXX", "ZZZ"};
      var versionMap = new Dictionary<string, int>();

      foreach (var s in strings) {
        if (!versionMap.ContainsKey(s))
          versionMap[s] = versionMap.Any() ? versionMap.Values.Max() + 1 : 1;
      }

      foreach (var s in strings)
        Console.WriteLine("{0} : {1}", s, versionMap[s]);
    }
  }
}





希望这有帮助,

Fredrik



Hope this helps,
Fredrik


我会做这样的事情:



Hi, I would do something like this:

var count = new Dictionary<string, int>();
var version = new Dictionary<string, int>();
foreach (string s in arrStrings)
{
    if (count.ContainsKey(s.ToString()))
    {
        count[s.ToString()]++;
    }
    else
    {
        count.Add(s.ToString(), 1);
        version.Add(s, version.Count + 1);
    }
}
foreach (var item in count)
{
    Console.WriteLine("{0} : {1}", item.Key, item.Value);
}
Console.WriteLine("############");
foreach (var item in version)
{
    Console.WriteLine("{0} : {1}", item.Key, item.Value);
}


使用字典是一个好主意。请尝试以下代码

Using a dictionary is a good idea. Try the following code
var di = new Dictionary<string, int>();

string[] sa = { "YYY", "XXX", "YYY", "ZZZ", "XXX", "ZZZ" };
int[] id = new int[sa.Length];
int cur_id = 1;

for (int n = 0; n<sa.Length ;++n)
{
  if (di.ContainsKey(sa[n]))
  {
    id[n] = di[sa[n]];
  }
  else
  {
    di.Add(sa[n], cur_id);
    id[n] = cur_id;
    ++cur_id;
  }
}


这篇关于比较字符串和版本化它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆