Uva Ducci Sequence(map映射的哈希表)

//map映射的哈希表,因为设置的哈希表的疏密程度显著影响查找的时长,比如说下面两个一个用了1190ms一个3000ms超时


//队列存储,循环取第一个放到后面,差放在后面
//遍历判断,全为0终止
//不全为0  
#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
vector<int> vect;
map<int,set<vector<int> > > mp;
int jud(vector<int> vect,int all){
    set< vector<int> > st=mp[all];
    if(st.count(vect))return 2;
    if(all==0)return 1;
    mp[all].insert(vect);
    return 0;
}
int main(){
    int a,b,c;
    scanf("%d",&a);
    while(a--){
        //初始化
        vect.clear();
        mp.clear();
        //data
        scanf("%d",&b);
        for(int i=0;i<b;i++){
            scanf("%d",&c);
            vect.push_back(c);
        }
        
        //solve
        while(1){
            long long all=0;//和
            vector<int> vect1;//queue
            for(int i=0;i<vect.size()-1;i++){
                vect1.push_back(abs(vect[i]-vect[i+1]));
                all+=vect[i]*(i+1);
            }
            vect1.push_back(abs(vect[0]-vect[vect.size()-1]));
            vect=vect1;
            int r=jud(vect,all);
            if(r==1){
                printf("ZERO\n");
                break;
            }
            else if(r==2){
                printf("LOOP\n");
                break;
            }
        }
    }
    return 0;
}

//2

//读取一行,读取每个并记录当前位置并检查是否大于已有标准vector的元素个数,大就push,小就比较对应,大就替换;
//获得vector模板后按照模板输出;
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

vector<string> vect[1005];
vector<int> jud;
int main(){
    string s,s1;
    int a,b=0;
    char chr;
    scanf("%c",&chr);
    while(chr!=EOF){
        s+=chr;
        int num=0;
        while(chr!='\r'&&chr!='\n'){
            scanf("%c",&chr);
            if(chr==' '){
                num++;
                if(num>jud.size()) jud.push_back(s.size());
                if(vect)
                vect[i].push_back(s);
                s.clear();
            }
            s+=chr;
        }
    }
    for(int i=0;i<b;i++){
        for(int j=0;j<vect[i].size();j++){
            cout<<vect[i][j];
            for(int k=0;k<jud[j]+1-vect[i][j].size();k++)
                cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值