求程序运行总时间,不太懂
刚刚看无意点进了那个什么英雄会,然后就去做了这道题
《微软必应·英雄会第三届在线编程大赛:几个bing?》
附上链接
http://hero.csdn.net/Question/Details?ID=215&ExamID=210
下面是我写出来的代码,提交后提示我程序运行总时间大于3S,最后挑战失败。
代码附上
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
public class Test
{
public static long howmany(string s)
{
char[] charList = s.ToCharArray();
List<int> b = new List<int>();
List<int> i = new List<int>();
List<int> n = new List<int>();
List<int> g = new List<int>();
for (int j = 0; j < charList.Length; j++)
{
if (charList[j] == 'b')
b.Add(j + 1);
else if (charList[j] == 'i')
i.Add(j + 1);
else if (charList[j] == 'n')
n.Add(j + 1);
else if (charList[j] == 'g')
g.Add(j + 1);
}
long count = 0;
for (int j = 0; j < b.Count; j++)
{
for (int k = 0; k < i.Count; k++)
{
if (i[k] > b[j])
{
for (int m = 0; m < n.Count; m++)
{
if (n[m] > i[k])
{
for (int t = 0; t < g.Count; t++)
{
if (g[t] > n[m])
{
count++;
}
}
}
}
}
}
}
long sum = count % 1000000007; //对10^9 + 7取余
return sum;
}
public static void Main()
{
Console.WriteLine(howmany("iinbinbing"));
}
}
using System;
public class Test
{
private static char[] find = { 'b', 'i', 'n', 'g' };
private static int foo(string s, int findindex)
{
int r = 0;
int p1 = s.IndexOf(find[findindex]);
if (findindex == 3)
{
for (int i = 0; i < s.Length; i++)
{
if (s[i] == 'g') r++;
}
return r;
}
if (p1 >= 0)
{
int p2 = s.IndexOf(find[findindex], p1 + 1);
r += foo(s.Substring(p1 + 1), findindex + 1);
if (p2 >= 0)
{
r += foo(s.Substring(p2), findindex);
}
}
return r;
}
public static int howmany(string s)
{
return foo(s, 0);
}
//start 提示:自动阅卷起始唯一标识,请勿删除或增加。
public static void Main()
{
Console.WriteLine(howmany("iinbinbing"));
}
//end //提示:自动阅卷结束唯一标识,请勿删除或增加。
}
using System;
public class Test
{
private static char[] find = { 'b', 'i', 'n', 'g' };
private static int foo(string s, int findindex)
{
int r = 0;
int p1 = s.IndexOf(find[findindex]);
if (findindex == 3)
{
for (int i = 0; i < s.Length; i++)
{
if (s[i] == 'g') r++;
}
return r;
}
if (p1 >= 0)
{
int p2 = s.IndexOf(find[findindex], p1 + 1);
r += foo(s.Substring(p1 + 1), findindex + 1);
if (p2 >= 0)
{
r += foo(s.Substring(p2), findindex);
}
}
return r;
}
public static int howmany(string s)
{
return foo(s, 0);
}
//start 提示:自动阅卷起始唯一标识,请勿删除或增加。
public static void Main()
{
string s = "";
Random r = new Random();
for (int i = 0; i < 500; i++)
s += find[r.Next(0, 4)];
DateTime dt1 = DateTime.Now;
Console.WriteLine(s);
Console.WriteLine(howmany(s));
Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - dt1.Ticks).TotalSeconds);
}
//end //提示:自动阅卷结束唯一标识,请勿删除或增加。
}