这题做的太闹心了,把vector爆了。。。debug了2个小时。。
虽然vector的maxsize有这么大,我算的也不会爆。
但如果init函数写成如下就会wa,希望路过的能指点下:
save.clear() ;int temp_max = '0' ;for(int i = 0 ; i < n ; i ++ ){if(read[i] > temp_max) {temp_max = read[i] ;save.clear() ;save.push_back(i) ;}else if(read[i] == temp_max){save.push_back(i) ;}}while(!q.empty()) q.pop() ;for(int i = 0 ; i < save.size() ; i ++ ){q.push(node(save[i] , 1)) ;}
思路,由于他是每个结点都能有一个下一个结点既有n条边,n个点,所以每个联通分量一定有环,只要从最大的值开始bfs即可,剪枝:比这层已经搜到的值小的减掉,这层已经搜到这个位置的减掉(即前面的值一样,之后的位置也会一样)
Ac代码:
#include <bits/stdc++.h>using namespace std;
typedef long long ll;
const int maxn = 150005 ;char read[maxn] ;
struct node{ll index , step ;node(ll index , ll step){this->index = index ;this->step = step ;}bool operator < (const node & k ) const {if(step == k.step) return read[index] < read[k.index] ;return step > k.step ;}};
priority_queue<node> q ;char ans[maxn] ;
bool vis[maxn] ;
vector<char> save ;
ll sta[maxn] , top ;
void init(ll n){char temp_max = 0 ;top = 0 ;for(ll i = 0 ; i < n ; i ++ ){if(read[i] > temp_max) {temp_max = read[i] ;top = 0 ;sta[top ++] = i ;}else if(read[i] == temp_max){sta[top ++] = i ;}}while(!q.empty()) q.pop() ;for(ll i = 0 ; i < top ; i ++ ){q.push(node(sta[i] , 1)) ;}memset(vis , 0 , sizeof(vis)) ;memset(ans , 0 , sizeof(ans)) ;top = 0 ;
}
int main(){//cout << save.max_size() << endl ;ll kase ; scanf("%lld" , &kase) ;ll tt = 1 ;while(kase -- ){ll n ; scanf("%lld" , &n) ;scanf("%s" , read) ;init(n) ;ll now_step = 1 ;while(!q.empty()){node t = q.top() ;q.pop() ;if(t.step != now_step){now_step = t.step ;while(top) vis[sta[--top]] = 0 ;}//int p = ((ll)t.index*(ll)t.index + 1) % (ll)n ;if(ans[t.step] > read[t.index] || vis[t.index] || t.step > n) continue ;vis[t.index] = true ;sta[top ++] = t.index ;ans[now_step] = read[t.index] ;q.push(node((t.index*t.index + 1) % n , t.step + 1)) ;}printf("Case #%lld: " , tt ++ ) ;printf("%s\n" , ans + 1) ;}return 0 ;
}