uva--1368(贪心,字符串模拟)

点击打开链接

该题是一个带有贪心思想的字符串模拟题,题目给定m个长度为n的字符串,让你求一个长度为n的字符串,使得该字符串与这m个字符串对应位置的字符不同的个数和最小。

要使对应位置不同字符最少,则该字符串每个字符优先选择该位置出现次数多的字符,若次数相同则选择字典序更小的字符。

代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#define from(i,a,n)   for(int i=a;i<n;i++)
#define refrom(i,n,a) for(int i=n;i>=a;i--)
#define EPS 1e-10
#define mod 1000000007
using namespace std;

const double INF=0x3f3f3f3f;
const int MAX =1000+10;
int m,n,pos,num;
char s[52][MAX],s1[MAX],s2[51];//s1用来记录字典序最小的符合条件的序列,s2用来处理每一个位置的字符
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>m>>n;
        int dis=0;
        memset(s1,0,sizeof(s1));
        num=0;
        //getchar();
        from(i,0,m)
        cin>>s[i];
        from(i,0,n)
        {
            pos=0;
            int maxlen=-INF,cnt=1;
            char c;
            from(j,0,m)
            s2[pos++]=s[j][i];//将第i个位置的所有字符取出
            sort(s2,s2+pos);//按升序排序,那么最先找到的一定是字典序小的字符
            c=s2[0];
            from(j,1,pos)
            {
                if(s2[j]==s2[j-1]) cnt++;
                else
                {
                    if(maxlen<cnt) {maxlen=cnt;c=s2[j-1];}//只有在字符出现次数个数小时才改变,否则不改变,这样保证了最小字典序的条件
                    cnt=1;
                }
            }
            if(maxlen<cnt) {maxlen=cnt;c=s2[pos-1];}//跳出循环后还要判断一次
            s1[num++]=c;
            dis+=(m-maxlen);总共字符个数减去最大出现次数就是该位置字符对于总的距离的贡献
            }
            cout<<s1<<endl<<dis<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 03:51:11

uva--1368(贪心,字符串模拟)的相关文章

UVA 1368 DNA(模拟+贪心)

DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nucleotides, namely Adenine, Thymine, Guanine, and Cytosine as shown in Figure 1. If we represent a nucleotide by its initial character

UVA 706 LCD Display 液晶显示屏 (字符串模拟)

[题目链接]click here~~ [题目大意] 给定的数字序列,按照要求输出对应液晶显示屏上的数字 输入: 2 12345 3 67890 0 0 输出: -- -- -- | | | | | | | | | | | | -- -- -- -- | | | | | | | | | | -- -- -- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- | | | | | | | |

uva 1368 - DNA Consensus String(字符串处理)

uva 1368 - DNA Consensus String Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nucleotides, namely Adenine, Thymine, Guanine, and Cytosine as shown in Figure 1. If we repre

UVA 10815-Andy&#39;s First Dictionary(字符串模拟+排序+重复删除)

Andy's First Dictionary Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i

hdu 4119 Isabella&#39;s Message【字符串模拟】

题目链接:http://write.blog.csdn.net/postedit 自我感觉比较麻烦 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<string> #include<map> using namespace std; const int maxh=100+10; const int maxe=100

大数运算之字符串模拟

相信大家被特别大的两个数据做运算折磨过.当两个操作数或者运算结果超过类型的表示范围后会有意想不到的错误,这时候我们的电脑还不如我们高中用过的科学计算器,这是作为一个程序员所不能忍受的.所以我们得找到其他的方式来计算.这就是我们今天要讨论的字符串模拟大数运算. 我们的运算一般使用int类型来算的,那么首先我们先复习一下各种int类型的数据表示范围: unsigned int 0-4294967295    int   -2147483648-2147483647  unsigned long 0-

UVA - 11584 划分字符串的回文串子串; 简单dp

/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单dp 题目大意: 给一个字符串, 要求把它分割成若干个子串,使得每个子串都是回文串.问最少可以分割成多少个. 定义:dp[i]表示前0~i内的字符串划分成的最小回文串个数: dp[i] = min(dp[j]+1 | j+1~i是回文串); 先预处理flag[i][j]表示以i~j内的字符串为回文串

从1打印到最大的n位数字(字符串模拟数字自加)

陷阱:  用最大的n位数-1(数字太大可能产生越界) 应该采用字符串模拟数字自加! 代码如下: #include<iostream> using namespace std; int  IsMax(char *number) {  int nLength = strlen(number);  int CarryBit = 0;  bool  ret = false;  for (int i = nLength-1; i >= 0; i--)  {   int nSum = number[