/* -*- coding: utf-8 -*- * * 2745.cc: No.2745 String Swap Battle - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_L = 100000; /* typedef */ using vi = vector; /* global variables */ char s[MAX_L + 4]; string ss[MAX_N]; /* subroutines */ void opr1(string &t) { int n = t.size(); vi cvs[26]; for (int i = 0; i < n; i++) cvs[t[i] - 'a'].push_back(i); for (int i = 0; i < n; i++) { int ti = t[i] - 'a'; for (int c = 0; c < ti; c++) if (! cvs[c].empty() && i < cvs[c].back()) { swap(t[i], t[cvs[c].back()]); return; } } for (int c = 0; c < 26; c++) if (cvs[c].size() >= 2) return; swap(t[n - 2], t[n - 1]); } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s); ss[i] = string(s); } string mint(1, 'z' + 1); int x = 0; for (int i = 0; i < n; i++) { opr1(ss[i]); if (mint > ss[i]) mint = ss[i], x = 1; else if (mint == ss[i]) x++; } for (int i = 0; i < n; i++) printf("%d\n", (mint == ss[i]) ? n + 1 - x : 0); return 0; }