1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 namespace DataStructure
8 {
9 class Client
10 {
11 //算法要求:求得所有包含在串s中而不包含在串t中的字符(s中重复的字符只选一个)
12 //构成的新串r,以及r中每个字符在s中第一次出现的位置
13
14 /// <summary>
15 /// 主方法
16 /// </summary>
17 public static void Main()
18 {
19 //字符串S
20 string str_S = "12345678abcdefgh345aefopqwer";
21 //字符串T
22 string str_T = "78aber";
23 //字符List,存放新串r中的字符
24 List<char> resultList= new List<char>();
25 //存放r中字符第一次在s中出现的位置
26 List<int> posList = new List<int>();
27 bool isExist;
28
29 //将两个字符串转到字符数组中存放
30 char[] arr_S = str_S.ToCharArray();
31 char[] arr_T = str_T.ToCharArray();
32
33 for (int i = 0; i < arr_S.Length; i++)
34 {
35 isExist = false;
36 for (int j = 0; j < arr_T.Length; j++)
37 {
38 if (arr_S[i] == arr_T[j])
39 {
40 isExist = true;
41 //在t中存在,则结束本趟循环
42 continue;
43 }
44 }
45
46 //保存符合条件的字符
47 if (!isExist)
48 {
49 resultList.Add(arr_S[i]);
50 posList.Add(i);
51 }
52 }
53
54 //输出显示
55 for (int k = 0; k < posList.Count; k++)
56 {
57 Console.WriteLine("字符:"+resultList[k]+"在s中的位置:"+posList[k]);
58 }
59 Console.Read();
60 }
61 }
62 }

下面是测试结果(控制台程序):

http://images.cnblogs.com/cnblogs_com/falconshh/301631/r_Snap3.jpg

posted on 2011-05-26 11:39  香椿炒鸡蛋  阅读(2126)  评论(0编辑  收藏  举报