diff --git a/13307130414/assignment11/GREAT SWERC.cpp b/13307130414/assignment11/GREAT SWERC.cpp new file mode 100644 index 0000000..2305401 --- /dev/null +++ b/13307130414/assignment11/GREAT SWERC.cpp @@ -0,0 +1,74 @@ +#include +#include +using namespace std; + +bool str[26]; // to mark if a letter has existed +bool num[10]; // to mark if a number is used +string buf[10]; +int a[10]; +int cnt; // count how many different characters +int ans; + +struct Node { + char c; + int n; + bool flag; // to mark if this letter is the leftmost one + Node() : flag(false) {} +} node[10]; + +bool check() { + for (int i = 0; i < cnt; i++) { + if (node[i].flag == true && a[i] == 0) + return false; + } + int temp = 0; + for (int i = 0; i < cnt; i++) + temp += a[i] * node[i].n; + if (temp == 0) return true; + else return false; +} + +void dfs(int i) { + if (i > cnt) return; + for(int j = 0; j < 10; j++) { + if (i == 0) memset(num, false, sizeof(num)); + if (num[j] == true) continue; + a[i] = j; + num[j] = true; + if (i == cnt-1 && check()) ans++; + dfs(i+1); + num[j] = false; + } +} + +int main() { + int n; + cin >> n; + memset(str, false, sizeof(str)); + cnt = 0; + ans = 0; + for (int i = 0; i < n; i++) + cin >> buf[i]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < buf[i].length(); j++) { + int t; + if (str[buf[i][j]-'A'] == true) { + for (t = 0; node[t].c != buf[i][j]; t++) ; + } + else { + t = cnt; + str[buf[i][j]-'A'] = true; + cnt++; + } + node[t].c = buf[i][j]; + int temp = 1; + for (int l = 0; l < buf[i].length()-j-1; l++) + temp *= 10; + if (i != n-1) node[t].n += temp; + else node[t].n -= temp; + if (j == 0) node[t].flag = true; + } + } + dfs(0); + cout << ans << endl; +} diff --git a/13307130414/assignment11/POJ 3364 Black and white painting.cpp b/13307130414/assignment11/POJ 3364 Black and white painting.cpp new file mode 100644 index 0000000..748becf --- /dev/null +++ b/13307130414/assignment11/POJ 3364 Black and white painting.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main() { + //freopen("3364.txt", "r", stdin); + int n,m,c; + while (1) { + cin >> n >> m >> c; + if (n == 0 && m == 0 && c == 0) return 0; + if (n < 8 || m < 8) { + cout << "0" << endl; + continue; + } + int ans = 0; + if (c == 1) { + ans = ((m-7)*(n-7)+1)/2; + } + else { + ans = ((m-7)*(n-1-7)+1)/2; + ans += (m-7)/2; + } + cout << ans << endl; + } +} diff --git a/13307130414/assignment11/POJ 3366 Deli Deli.cpp b/13307130414/assignment11/POJ 3366 Deli Deli.cpp new file mode 100644 index 0000000..6fd4bf8 --- /dev/null +++ b/13307130414/assignment11/POJ 3366 Deli Deli.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +using namespace std; + +const int H = 100007; +int hashtable[H]; + +struct Node { + char before[25]; + char after[25]; + int next; + Node() : next(-1) {} +} node[25]; + +int ELFhash(char* key) { + unsigned long g,h=0; + while(*key) + { + h=(h<<4)+*key++; + g=h & 0xf0000000L; + if(g) h^=g>>24; + h&=~g; + } + return h%H; +} + +void inserthash(char *s, int i) { + int h = ELFhash(s); + node[i].next = hashtable[h]; + hashtable[h] = i; +} + +int searchhash(char *s) { + int h = ELFhash(s); + int next = hashtable[h]; + while (next != -1) { + if (!strcmp(node[next].before, s)) return next; + next = node[next].next; + } + return -1; +} + +int main() { + //freopen("3366.txt", "r", stdin); + int l,n; + cin >> l >> n; + memset(hashtable, -1, sizeof(hashtable)); + for (int i = 0; i < l; i++) { + cin >> node[i].before >> node[i].after; + inserthash(node[i].before, i); + } + for (int i = 0; i < n; i++) { + char str[25]; + cin >> str; + if (searchhash(str) != -1) { + cout << node[searchhash(str)].after << endl; + } + else if (str[strlen(str) - 1] == 'y') { + if (strlen(str) > 1 && str[strlen(str) -2] == 'a' || str[strlen(str) -2] == 'e' || + str[strlen(str) -2] == 'i' || str[strlen(str) -2] == 'u' || str[strlen(str) -2] == 'o') + cout << str << 's' << endl; + else { + str[strlen(str) - 1] = '\0'; + cout << str << "ies" << endl; + } + } + else if (str[strlen(str)-1] == 'o' || str[strlen(str)-1] == 's' || str[strlen(str)-1] == 'x' + || (str[strlen(str)-1] == 'h' && (str[strlen(str)-2] == 'c' || str[strlen(str)-2] == 's'))) { + cout << str << "es" << endl; + } + else + cout << str << "s" << endl; + } + return 0; +} diff --git a/13307130414/assignment11/POJ 3367 Expressions.cpp b/13307130414/assignment11/POJ 3367 Expressions.cpp new file mode 100644 index 0000000..7eeb9e3 --- /dev/null +++ b/13307130414/assignment11/POJ 3367 Expressions.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const int N = 10010; + +vector v; +char str[N]; +pair p[N]; +int len; + +void init() { + len = strlen(str); + for (int i = 0; i < len; i++) + p[i].first = p[i].second = -1; + v.clear(); +} + +void output() { + string ans = ""; + queue q; + q.push(len-1); + while(!q.empty()) { + int t = q.front(); + q.pop(); + ans += str[t]; + if (p[t].first != -1) { + q.push(p[t].first); + q.push(p[t].second); + } + } + reverse(ans.begin(), ans.end()); + cout << ans << endl; +} + +int main() { + //freopen("3367.txt", "r", stdin); + int cas; + cin >> cas; + while(cas--) { + cin >> str; + init(); + for (int i = 0; i < len; i++) { + if (islower(str[i])) { + v.push_back(i); + } + else { + int a = v.back(); + v.pop_back(); + int b = v.back(); + v.pop_back(); + p[i] = make_pair(b,a); + v.push_back(i); + //cout << str[i] << ' ' << str[b] << ' ' << str[a] << endl; + } + } + output(); + } +} diff --git a/13307130414/assignment11/POJ 3370 Halloween treats.cpp b/13307130414/assignment11/POJ 3370 Halloween treats.cpp new file mode 100644 index 0000000..09364ed --- /dev/null +++ b/13307130414/assignment11/POJ 3370 Halloween treats.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; + +int mod[100010]; +int a[100010]; + +int main() { + int c,n; + while(1) { + scanf("%d%d", &c, &n); + if (c == 0 && n == 0) return 0; + memset(mod, -1, sizeof(mod)); + long long sum = 0; + for (int i = 0; i < n; i++) + scanf("%d", &a[i]); + int i; + for (i = 0; i < n; i++) { + sum += a[i]; + if (sum%c == 0) { + for (int j = 1; j <= i+1; j++) { + printf("%d", j); + if (j != i+1) + printf(" "); + } + printf("\n"); + break; + } + else if (mod[sum%c] != -1) { + for (int j = mod[sum%c]+1; j <= i+1; j++) { + printf("%d", j); + if (j != i+1) + printf(" "); + } + printf("\n"); + break; + } + else + mod[sum%c] = i+1; + } + if (i == n) printf("no sweets\n"); + } +}