小编use*_*540的帖子

改进元素去除算法

问题

给定一个字符串sm查询.对于每个查询,删除K第 - 次出现的字符x.

例如:

abcdbcaab
5
2 a
1 c
1 d
3 b
2 a

Ans abbc
Run Code Online (Sandbox Code Playgroud)

我的方法

我正在使用BIT树进行更新操作.

码:

for (int i = 0; i < ss.length(); i++) {

    char cc = ss.charAt(i);
    freq[cc-97] += 1;
    if (max < freq[cc-97]) max = freq[cc-97];
    dp[cc-97][freq[cc-97]] = i;                 // Counting the Frequency
}
BIT = new int[27][ss.length()+1];
int[] ans = new int[ss.length()];
int q = in.nextInt();
for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

java algorithm

7
推荐指数
1
解决办法
182
查看次数

TSP使用动态编程

我正在学习TSP并发现了TSP的这种递归解决方案

int compute(int start,int set)
{   int masked,mask,result=INT_MAX,temp,i;//result stores the minimum 
    if(g[start][set]!=-1)//memoization DP top-down,check for repeated subproblem
        return g[start][set];
    for(i=0;i<n;i++)
        {   //npow-1 because we always exclude "home" vertex from our set
            mask=(npow-1)-(1<<i);//remove ith vertex from this set
            masked=set&mask;
            if(masked!=set)//in case same set is generated(because ith vertex was not present in the set hence we get the same set on removal) eg 12&13=12
            {   
                temp=adj[start][i]+compute(i,masked);//compute the removed set
                if(temp<result)
                    result=temp,p[start][set]=i;//removing ith vertex gave us minimum
            }
        }
        return g[start][set]=result;//return minimum
} …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm traveling-salesman

-3
推荐指数
1
解决办法
9546
查看次数

标签 统计

algorithm ×2

c++ ×1

java ×1

traveling-salesman ×1