SRM 405 DIV2

Problem Statement

    

An ideal string is a string where the 1-based index of the first occurrence of each letter is equal to the number of occurrences of that letter in the string. For example, the "BAOOOA" is an ideal string (quotes for clarity only). The letter 'B' appears 1 time, and its index is 1. The letter 'A' occurs 2 times and its first index is 2. The letter 'O' occurs 3 times and its first index is 3.

Given an int length, return the lexicographically smallest ideal string of that length containing only uppercase letters ('A'-'Z'). If there are no such ideal strings of that length, return an empty string instead.

Definition

    
Class: IdealString
Method: construct
Parameters: int
Returns: string
Method signature: string construct(int length)
(be sure your method is public)
    
 

Notes

- String A is lexicographically smaller than string B of the same length if it contains a smaller letter at the first position they differ. Letter X is smaller than letter Y if it comes earlier in the alphabet.

Constraints

- length will be between 1 and 100, inclusive.

Examples

0)  
    
1
Returns: "A"
 
1)  
    
3
Returns: "ABB"
 
2)  
    
2
Returns: ""
There's no way we can construct an ideal string of length 2 - if both its characters are equal, then the number of occurrences (2) and the position of its first occurrence (1) do not match; if they are distinct, then for the second character the number of occurrences (1) and the position of its first occurrence (2) do not match as well.
3)  
    
6
Returns: "ABCBCC"
We can permute the last 3 characters in any way, but this way gets us the lexicographically smallest answer.
4)  
    
7
Returns: "ABBCCCC"
 
5)  
    
5
Returns: ""
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

这场比赛其实不是很难,3道题都提交上去了。但是很遗憾 后面2道题竟然都错了,很无语。

这个是第三道题,也不是很难,基本是个模拟的方法,主要控制的元素就是某个字母出现的第一个位置。它剩下的字母后面肯定是添加在字符串的最后。但是注意的是,原来用的是Dictionary<int,string>可是竟然没有ling的帮助,Dictionary的内置函数用的很不方便,所以后来又改成string数组了。总之这道题和第二题一样都是比较简单,只可惜都做错了,很有点小郁闷。

 

using System;

using System.Collections.Generic;

//using System.Linq;

using System.Collections;

using System.Text;







    class IdealString

    {

        string[] l = new string[101];

        //string word =new string[26]{"A","B","C","D","E","F","G","H","I","K"}

        //Dictionary<int, string> word = new Dictionary<int, string>();

        //List<string> word = new List<string>();

        StringBuilder[] word = new StringBuilder[101];



        public string construct(int length)

        { 

            //l[1]="A";

            word[1] = new StringBuilder("A");

            int num=0;

            for (int i = 2; i <= 100; i++)

                word[i] = new StringBuilder("");

                for (int i = 1; i <= length; i++)

                {

                    string s = word[i].ToString();

                    if (s == "") continue;

                    //int le = word.ElementAt(i).Key;

                    for (int u = 0; u < s.Length; u++)

                    {

                        if (s[u] > num) num = s[u];

                    }

                    num -= 'A';

                    int j, ok = 0;

                    for (j = 0; j < s.Length; j++)

                    {

                        if (ok == 0 && s[j] != Convert.ToChar('A' + num)) continue;

                        ok = 1;

                        string ss = s.Insert(j + 1, Convert.ToString(Convert.ToChar('A' + num + 1)));

                        for (int k = 0; k < j + 1; k++)

                        {

                            ss += Convert.ToString(Convert.ToChar('A' + num + 1));

                        }

                        if (ss.Length > 100) break;

                        if (word[ss.Length].Length!=0)

                        {

                            if (ss.CompareTo(word[ss.Length].ToString()) < 0)

                            {

                                word[ss.Length] = new StringBuilder(ss);

                            }

                        }

                        else

                        {

                            word[ss.Length] = new StringBuilder(ss);

                        }



                    }

                    //num++;

                }

            //for (int i = 0; i < word.Count; i++)

            //{

            //    if (word.ElementAt(i).Value.Length == length)

            //        return word.ElementAt(i).Value;

            //    if (word.ElementAt(i).Value.Length > length)

            //        return "";

            //}

            for(int j=word.Length-1;j>=0;j--)

            {

                if (word[j].Length == length)

                    return word[j].ToString();

                if (word[length].Length ==0)

                    return "";

            }



            return "";

            

        }

    }
总体感觉这场比赛还是学到了,昨天还是仔细,再仔细,再仔细。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值