1112. Stucked Keyboard (20) -- 字符串处理,使用map

来源:互联网 发布:数据库第六版中文答案7 编辑:程序博客网 时间:2024/06/07 20:25

题目地址

https://www.patest.cn/contests/pat-a-practise/1112

题目描述

On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for k times.

Now given a resulting string on screen, you are supposed to list all the possible stucked keys, and the original string.

Notice that there might be some characters that are typed repeatedly. The stucked key will always repeat output for a fixed k times whenever it is pressed. For example, when k=3, from the string “thiiis iiisss a teeeeeest” we know that the keys “i” and “e” might be stucked, but “s” is not even though it appears repeatedly sometimes. The original string could be “this isss a teest”.

Input Specification:

Each input file contains one test case. For each case, the 1st line gives a positive integer k ( 1

3caseee1__thiiis_iiisss_a_teeeeeest

Sample Output:

eicase1__this_isss_a_teest

ac代码(2017-2-11)

自己构造一个数据结构,遍历字符串的时候

struct data{    char c; // 字符    int cnt; // 字符连续的次数};

同时得到每个字符出现的先后顺序,方便后面的输出排序

map< char,vector< int >> mp; 存储某个字符所有的类似data的数据,因为某个字符出现错误,一定是所有连续位置都错误(不是有些位置错误,有些位置正确)

#include <cstdint>#include <cstdio>#include <memory>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> using namespace std;// freopen("in.txt", "r", stdin);#define INF 0x7fffffffconst int N = 1000 + 5;int k;char s[N];int occurIndex[N];struct data{    char c;    int cnt;};vector<data> v;int main(int argc, char *argv[]) {       freopen("in.txt", "r", stdin);    while( scanf("%d", &k) != EOF)    {        scanf("%s", s);        int len = strlen(s); // s最后字符处理的小技巧        s[len] = 'A';        s[len +1] = '\0';        len ++;        for(int i=0;i<N;i++)        {            occurIndex[i] = -1;        }        map<char,vector<int>> mp;        char pre = ' ';        int cnt = 0;        for(int i=0;i<len;i++)        {            if(s[i] == pre){                cnt ++;            }else{                if(pre != ' ')                {                    mp[pre].push_back(cnt);                     data dt;                    dt.c = pre;                    dt.cnt = cnt;                    v.push_back(dt);                }                pre = s[i];                cnt = 1;            }            if(occurIndex[s[i]] == -1)            {                occurIndex[s[i]] = i;            }        }        vector<bool> vis(N, false);        vector<char> ans;        map<char,vector<int>>::iterator it = mp.begin();        while(it != mp.end())        {            char c = it->first;            vector<int> v = it->second;            int cnt = v.size();            bool flag = true;            for(int i = 0; i < cnt; i++)            {                if(v[i] % k != 0)                    flag = false;            }            if(flag)            {                ans.push_back(c);                vis[c] = true;            }            ++it;        }        sort(ans.begin(), ans.end(), [=](char c1,char c2)        {            return occurIndex[c1] < occurIndex[c2];          });        int ansLen = ans.size();        for(int i=0;i<ansLen;i++)        {            printf("%c", ans[i]);        }        printf("\n");        // 处理结果        int vLen = v.size();        for(int i=0;i<vLen;i++)        {            if(vis[ v[i].c ])            {                v[i].cnt /= k;            }        }        for(int i=0;i<vLen;i++)        {            for(int j=0;j<v[i].cnt;j++)            {                printf("%c", v[i].c);            }        }        printf("\n");    }    //printf("\n"););    return 0;}
0 0
原创粉丝点击