美文网首页
poj1016 string压缩

poj1016 string压缩

作者: 暖昼氤氲 | 来源:发表于2019-11-01 15:22 被阅读0次
/*
Time:2019.11.1
Author: Goven
type:压缩存储:string处理+模拟 
err:
ref: to_string的写法参考https://blog.csdn.net/shutdown113/article/details/75231356 
    69-71参考https://blog.csdn.net/lyy289065406/article/details/6673675 
知识点:1.to_string的应用 (标准11有,poj没有 ) 
*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

string to_string(int a) {
    string s = "";
    if (a == 0) return "0";//err1
    while(a) {
        s += '0' + a%10;
        a /= 10;
    }
    reverse(s.begin(), s.end());
    return s;
}

string change(string n) {//dif:由于输入是string,返回string的时候很麻烦 
    int len = n.length();
    int a[10] = {0};
    for (int i = 0; i < len; i++) {
        a[n[i] - '0']++;
    }

    string m = "";
    for (int i = 0; i < 10; i++) {
        if (a[i] != 0) {
            m += to_string(a[i]) + to_string(i);
        }
    } 
    return m;
}

int main()
{
    string n, s;//Att: 这里n不能用int,ll?
    int i;
    
    while (cin >> n && n != "-1") {
        s = change(n);
        if (s == n) {
            cout << n << " is self-inventorying" << endl;
            continue;
        }
        
        vector<string> v;
        v.push_back(n); v.push_back(s);
        
        for (i = 1; i < 16; i++) {
            s = change(s);
            if (s == v[i]) {
                cout << n << " is self-inventorying after " << i << " steps" << endl;
                break;
            }
            v.push_back(s);
        }
        if (i > 15) {
            int mink = 20;
            for (int j = 0; j < 16; j++) {
                for (int k = j + 1; k < 16; k++) {
                    if (v[j] == v[k]) {
                        mink = k - j; break;
                    } 
//                  if (v[j] == v[k] && mink > (k - j)) {//一开始写的是这样的,但是根据ref中的第三种可知,没必要 
//                      mink = k - j;
//                  }
                }
            }
            
            if (mink < 20) cout << n << " enters an inventory loop of length " << mink << endl;
            else cout << n << " can not be classified after 15 iterations" << endl;
        }
    }
    return 0;
}

相关文章

网友评论

      本文标题:poj1016 string压缩

      本文链接:https://www.haomeiwen.com/subject/plofbctx.html