|
I have found this example but it is not 100 % good. Could you please have a look?
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
namespace ConsoleApplication1<br />
{<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
string _givenString = "wvtnfhxyz1hdxyz1fdxyz1ejxyz1dhxyz1dxyz1eeaa1oeys";<br />
Dictionary<string, int> _codes = new Dictionary<string, int>();<br />
int _startingPoint = 0;<br />
while (_startingPoint < _givenString.Length)<br />
{<br />
for (int j = 2; j < _givenString.Length; j++)<br />
{<br />
for (int i = _startingPoint; i < _givenString.Length && i + j < _givenString.Length; i++)<br />
{<br />
if (!_codes.ContainsKey(_givenString.Substring(i, j)))<br />
{<br />
_codes.Add(_givenString.Substring(i, j), 1);<br />
}<br />
else<br />
{<br />
_codes[_givenString.Substring(i, j)] += 1;<br />
}<br />
}<br />
}<br />
<br />
_startingPoint++;<br />
}<br />
<br />
List<KeyValuePair<string, int>> _sortedCodes = SortDictionary(_codes);<br />
for (int i = 0; i < 5; i++)<br />
{<br />
Console.WriteLine(_sortedCodes[i].Key + " : " + _sortedCodes[i].Value + " times");<br />
}<br />
Console.ReadKey();<br />
}<br />
<br />
public static List<KeyValuePair<string, int>> SortDictionary(Dictionary<string, int> data)<br />
{<br />
List<KeyValuePair<string, int>> result =<br />
new List<KeyValuePair<string, int>>(data);<br />
result.Sort(<br />
delegate(<br />
KeyValuePair<string, int> first,<br />
KeyValuePair<string, int> second)<br />
{<br />
return second.Value.CompareTo(first.Value);<br />
}<br />
);<br />
return result;<br />
}<br />
}<br />
}
modified on Thursday, September 17, 2009 8:13 AM
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CountString
{
class Program
{
static void Main(string[] args)
{
string _givenString = "wvtnfhxyz1hdxyz1fdxyz1ejxyz1dhxyz1dxyzeeaa1oeys";
Dictionary<string, int> _codes = new Dictionary<string, int>();
int _startingPoint = 0;
//while (_startingPoint < _givenString.Length)
//{
for (int j = 2; j < _givenString.Length; j++)
{
for (int i = _startingPoint; i < _givenString.Length && i + j < _givenString.Length+1; i++)
{
if (!_codes.ContainsKey(_givenString.Substring(i, j)))
{
_codes.Add(_givenString.Substring(i, j), 1);
}
else
{
_codes[_givenString.Substring(i, j)] += 1;
}
}
}
// _startingPoint++;
//}
List<KeyValuePair<string, int>> _sortedCodes = SortDictionary(_codes);
for (int i = 0; i < 5; i++)
{
Console.WriteLine(_sortedCodes[i].Key + " : " + _sortedCodes[i].Value + " times");
}
Console.ReadKey();
}
public static List<KeyValuePair<string, int>> SortDictionary(Dictionary<string, int> data)
{
List<KeyValuePair<string, int>> result =
new List<KeyValuePair<string, int>>(data);
result.Sort(
delegate(
KeyValuePair<string, int> first,
KeyValuePair<string, int> second)
{
return second.Value.CompareTo(first.Value);
}
);
return result;
}
}
}
does it work as you want??
|
|
|
|
|
Well, I'm sure there's a better algorithm for this, but here's one off the top of my head:
Note that this is NOT an efficient algorithm, and will NOT earn you any prizes if this is a school assignment (And if that's the case, keep in mind your teacher may be reading this).
1. Keep a string:int dictionary of substring:count
2. Loop through each character:
a. Looping from 2 to your maximum substring size (Say, 5)
i. Grab the substring of that length from the current position
ii. Add/increment it in the dictionary
3. Look through the dictionary keys:
a. See if the key is a substring of any other keys, with an equal count... If so, just delete this key.
Grab 'em, sort 'em, and display 'em...
If you want a better algorithm, you could try posting this in the Algorithms forum... I would guess there are at least a dozen formalized and documented algorithms to handle this, with all sorts of fancy names, that the folks there could point you to.
|
|
|
|
|
Thanks for you answer, but I dont understand it to make it in code. Could you please make an example code? many thanks
|
|
|
|
|
Hi,
I am developing a scripting tool in which I want to compile and execute the code in MEMORY, not as an executable file. But in there I need to redirect the console output to a Richtextbox and input to a simple textbox.
Example:
The script code is like this
public class Script
{
public static void Main()
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
When I compile and execute in memory I want to print "Hello World" in a richtextbox and input the key from a textbox to terminate the application.
How can I accomplish it? I know how to redirect input/output if it is an executable file, but I don't know how to do the same for in-memory execution. Can anyone please help me?
|
|
|
|
|
What is the benefit you are seeking by compiling and executing in memory? Does it outweigh the risks of potential security vulnerabilities this exposes?
If this needed, why compile an entire application, just use the particular method that is necessary for your functionality.
only two letters away from being an asset
|
|
|
|
|
That is not the point. OK instead of in-memory execution I invoke the entry point function from the compiled assembly. But there also how would I redirect the console input output to a textbox or richtextbox?
|
|
|
|
|
Anindya Chatterjee wrote: how would I redirect the console input output to a textbox or richtextbox?
You don't. Execute the method from the assembly, get the output, then assign it to the textbox in your application.
only two letters away from being an asset
|
|
|
|
|
Sorry, I didn't get that.
If the script has a line like:
Console.WriteLine("Hello World");
then how would I get the output and assign to the textbox? That entry-point function is not returning any string. If I am wrong please correct me with some example.
And also what about
Console.ReadKey() or Console.ReadLine(...) ?
Thanks and regards,
|
|
|
|
|
That's the point you can't do this. And why would you want to? If you have a textbox in your application and you need to pass the value from the textbox to an external application, then why do you need to try reassigning the input stream? Sounds more like you need to refactor the external application.
only two letters away from being an asset
|
|
|
|
|
Hi,
In datagridview, I want to change the color of particular gridline.
There is option to change all gridlines to any color but want to change particular gridline color.
Thanks in Advance.
GTag
|
|
|
|
|
Hello.
We got a newbie question on XML tags for documentation (only partly referring to C#):
The text needs to use the data type of a function (or a function parameter). The documentation text goes the same for all programming languages though you have different data type namings in C#, VB.NET, C++ ... As an example, in C# and C++ you have float data type but VB names it Single .
A code example (VB here):
''' <summary>The function <cref name="myFunction"/> has the data type Single/float.</summary>
Public Function myFunction As Single()
Return myFunction(Me.Count)
EndFunction
Can we refer to the data type naming as we do to the function name? Could be a tag like <creftype name="myFunction"/>
We use VS2008 (and Sandcastle for html help).
Thanks for hints
Guido Carl
modified on Tuesday, September 8, 2009 7:40 AM
|
|
|
|
|
float is an alias for System.Single. So you can write,
public float MyFunction()
{
}
|
|
|
|
|
Thanks. So easy if you know about it.
Guido
|
|
|
|
|
Hi everyone there, I searched alot but didnt get any source to find out how I can add Icons or images on the left side of my context menu or context menu strip. Any urgent reply will be appreciated.
Regards
|
|
|
|
|
Every ToolStripMenuItem you add to your context menu has an Image property.
|
|
|
|
|
I want to make an iphone style scrolling. I tryed to set the scroll position(in a loop) on mouse click but my application is flickering when doing that.
can anybody explain/solve my problem?
|
|
|
|
|
Often, setting the DoubleBuffer style of your Form or Panel will solve the flickering problem:
this.SetStyle (ControlStyles.DoubleBuffer, true);
|
|
|
|
|
|
Something like this works:
private const int WM_ENTERSIZEMOVE = 0x0231;
private const int WM_EXITSIZEMOVE = 0x0232;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_ENTERSIZEMOVE:
this.Opacity = 0.5;
break;
case WM_EXITSIZEMOVE:
this.Opacity = 1.0;
break;
}
this.Invalidate();
}
Still trying to get rid of the small flicker....
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
|
So...what's the question? How does that image relate?
|
|
|
|
|
|
This [^]may give you an idea.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
hi .
Please help me .I want to insert color in the line of the richtextbox.In fact when user clicked on the lines of the rtb , i want to changed background color of the that line .
|
|
|
|